reactjs前端实践|第一篇:调色板示例组件的使用

发布时间:2019-08-12 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了reactjs前端实践|第一篇:调色板示例组件的使用脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

背景

最近终于开始折腾前端,经过大量的阅读和研究,决定使用react.js及相关产品来架构我的前端技。我本是个纯后端,喜欢算法,几年前,发现了node.js,因为它开,底层是C++,正中我下怀,呵呵!

原因

技术栈

工具

  • create-react-app

在学习、开发阶段,有它真好。我发现,拖了这久我才行动起来,原来是我没有遇见create-react-app这个神器啊,呵呵。

实践一

color-card(调色板)示例

示例描述

显示一块调色板,上面一个正方形展现真实颜色,下面一个文字标签显示颜色的16进制编码,色值由父类单入口传递。

代码结构

整个App分解为三个组件,一个父组件(Card)、两个子组件(Square、Label),颜色值由Card组件的Props向子组件单向传递,样式使用styled-components来定义。

代码分析

父组件(Card)

import React, { Component } from 'react';
import styled from 'styled-components';
import Square from './Square';
import Label from './Label';

const CardDiv = styled.div`                                     //定义卡片div
    height: 200px;
    width: 150px;
    padding: 0,
    background-color: #FFF;
    -webkit-filter: drop-shadow(0px 0px 5px #666);              //这行可以删除
    filter: drop-shadow(0px 0px 5px #666);
`;
class Card extends Component {
    render() {
        return (
            <CardDiv>
                <Square color={this.PRops.color}/>            //传递Props
                <Label color={this.props.color}/>
            </CardDiv>
        );
    }
}

export default Card;

子组件(Square)

import React, { Component } from 'react';
import styled from 'styled-components';

const SquareDiv = styled.div`
    height: 150px;
    background-color: ${props => props.color};                   //styled-components中接收父组件传递来的参数的方式
    `;
class Square extends Component {
    render() {
        return (
            <SquareDiv {...this.props}></SquareDiv>              //延展符的使用,传递属性到styled-components中
        );
    }
}

export default Square;

子组件(Label)

import React, { Component } from 'react';
import styled from 'styled-components';

const P = styled.p`
        font-family: sans-serif;
        font-weight: bold;
        padding: 13px;
        margin: 0;
    `;

class Label extends Component {
    render() {
        return (
            <P>{this.props.color}</P>
        );
    }
}

export default Label;

组件加载(index.js)

@H_498_304@import Card from './components/Card'; ReactDOM.render( <Card color="#FFA737"/>, document.getElementById('root') );

项目地址

https://git.oschina.net/zhoutk/reactodo.git
https://github.COM/zhoutk/reactodo.git

使用方法

git clone https://git.oschina.net/zhoutk/reactodo.git
or git clone https://github.com/zhoutk/reactodo.git
cd reactodo
npm i
git checkout color-card
npm start

小结

这是第一篇,主要是技术选型思考,样式的处理思考了很久,最后决定用style-components,希望它不要让我失望。难点:父组件的属性传递到styled-components组件中的方式,它以子组件的方式来组织的,并采用了回调函数的方式,与JSX的直接使用方式有所不同,试验了好多次,才理解。后来想想,还是本人对前端的思维不熟练导致的,要加强实践,加油!

脚本宝典总结

以上是脚本宝典为你收集整理的reactjs前端实践|第一篇:调色板示例组件的使用全部内容,希望文章能够帮你解决reactjs前端实践|第一篇:调色板示例组件的使用所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。