深入react技术栈(1)

2017-08-16  本文已影响0人  吴林霏smile

第一章(基本介绍、JSX、React组件)

虚拟DOM
JSX
JSX基本语法
XML基本语法

+标签可以任意嵌套。可以清晰地看到DOM树状结构及其属性。

const List = () =>(
    <div>
        <Title>title</Title>
        <ul>
            <li>list</li>
            <li>list</li>
        </ul>
    </div>
);
元素类型
    const App={
        <Nav>
            {/* 节点注释*/}
            <Person
            /* 多行
            注释 */
            name={window.name}
        </Nav>
    }
元素属性

在JSX中,DOM和组件元素都有属性。

可以将
const data = {name:'foo',value:'bar'};
const component = <Component name={data.name} value={data.value} />;
写为
const data = {name:'foo',value:'bar'};
const component = <Component {...data}/>;
JavaScript属性表达式
HTML转义
React组件
React组件的构建
React组件的构建方法
    const Button = React.createClass({
        getDefaultProps(){
            return{
                color:'blue',
                text:'Confirm',
            };
        },

        render(){
            const {color,text} = this.props;
            return(
                <button className={`btn btn-${color}`}>
                    <em>{text}</em>
                </button>
            );
        }
    });
    
import React,{Component} form 'react';
class Button extends Component{
    constructor(props){
        super(props);
    }
    static defaultProps = {
        color:'blue',
        text:'Confirm',
    };
    render(){
        const {color,text} = this.props;
        return(
            <button className={`btn btn-${color}`}>
                <em>{text}</em>
            </button>
        );
    }
}
function Button({color='blue',text='Confirm'}){
    return(
        <button className={`btn btn-${color}`}>
            <em>{text}</em>
        </button>
    );
}
上一篇 下一篇

猜你喜欢

热点阅读