美文网首页
React小示例

React小示例

作者: wlianfu | 来源:发表于2016-10-11 10:35 被阅读17次

ES6模式的数据渲染

class LikeButton extends React.Component{
    constructor(){
        super();
        this.state = {
            liked: false
        };
        this.handleClick = this.handleClick.bind(this);
    }
    handleClick(){
        this.setState({liked: !this.state.liked});
    }
    render(){
        const text = this.state.liked ? 'liked' : 'haven\'t liked';
        return (
            <div onClick={this.handleClick}>
                You {text} this. Click to toggle.
            </div>
        );
    }
}

ReactDOM.render(
    <LikeButton />,
    document.querySelector('.example')
);
class HelloWorld extends React.Component{
    render(){
        return (
            <p>
                Hello, <input type='text' placeholder='Your name here' />
                It is {this.props.data.toTimeString()}
            </p>
        );
    }
}
function tick(){
    ReactDOM.render(
        <HelloWorld />,
        document.querySelector('.example')
    );
}
setInterval(tick, 1000);

相关文章

网友评论

      本文标题:React小示例

      本文链接:https://www.haomeiwen.com/subject/jfavyttx.html