React入门(二)

2018-10-09  本文已影响6人  梧可奈何

三、React组件

React 组件基本上是由组件的构建方式、组件内的状态属性与生命周期方法组成

用 React.createClass 构建组件是 React 最传统、也是兼容性最好的方法。在 0.14 版本发布之前,这一直都是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 } from '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>
        );
    }
}

四、React数据流

在 React 中,数据是自顶向下单向流动的,即从父组件到子组件。这条原则让组件之间的关系变得简单且可预测。

state与props是React组建中最重要的概念。如果顶层组件初始化props,那么React会乡下遍历整颗组件树,重新尝试渲染所有相关的子组件。而state只关心每个组件自己内部的状态,这些状态只能在组件内改变。把组件看成一个函数,那么它接受了props作为参数,内部由state作为函数的内部参数,返回一个Virtual DOM的实现。

state

state用于管理组件内部的状态,当state状态发生变化时,该组件就会尝试重新渲染。例如实现一个计数器的组件:

import React, { Component } from 'react';

class App extends Component {

  constructor(props){
    super(props);

    this.handleClick = this.handleClick.bind(this);
    this.state = {
      count: 0,
    };
  }

  handleClick(e) {
    e.preventDefault();

    this.setState({
        count: this.state.count + 1,
    });
  }

  render() {
    return (
      <div>
        <p>{this.state.count}</p>
          <a href='#' onClick={this.handleClick}>更新</a>
      </div>
    );
  }
}

export default App;

在React中常常在事件处理方法中更新state,上述例子就是通过点击“更新”按钮不断地更新内部count的值,这样就可以把组件内状态封装在实现中。

props

props是React用来让组件之间互相联系的一种机制,通俗的说就像方法参数一样。React的单向数据流,主要的流动管道就是props。

function HelloMessage(props) {
    return <h1>Hello {props.name}!</h1>;
}
 
const element = <HelloMessage name="Runoob"/>;
 
ReactDOM.render(
    element,
    document.getElementById('example')
);

五、React组件的生命周期

React组件的生命周期可分为三个状态:

生命周期的方法有:

组件挂载时


import React, { Component, PropTypes } from 'react';

class App extends Component{
  static propTypes = {
    // ...
  };
  
  static defaultPorps = {
      // ...
  };
  
  constructor(props) {
    super(props);
    
    this.state = {
      // ...
    };
  }
  
  componentWillMount(){
    // ...
  }
  
  componentDidMount(){
    // ...
  }
  
  render(){
    return <div>This is a demo</div>;
  }
  
  
}

componentWillMount和componentDidMount只会在组件初始化时运行一次。如果在componentWillMount方法中执行setState方法,组件会更新state,但是组件只渲染一次;如果在componentDidMount中执行setState方法,初始化时组件会渲染两次

组件更新时

import React, { Component, PropTypes } from 'react';

class App extends Component {
  componentWillReceiveProps(nextProps) {
    //
  }
  
  shouldComponentUpdate(nextProps, nextState) {
    //
  }
  
  componentWillUpdate(nextProps, nextState) {
    //
  }
  
  componentDidUpdate(preProps, prevState) {
    //
  }

  render(){
    return <div>This is a demo</div>;
  }
}

组件卸载时

import React, { Component, PropTypes } from 'react';

class App extends Component {
 componentWillUnmount(){
   // ...
 }

  render(){
    return <div>This is a demo</div>;
  }
}

生命周期整体流程

React生命周期整体流程图

React组件API

setState(object nextState[, function callback])
replaceState(object nextState[, function callback])
setProps(object nextProps[, function callback])
replaceProps(object nextProps[, function callback])
forceUpdate([function callback])
DOMElement findDOMNode()
bool isMounted()

参考:
《深入React技术栈》 陈屹峰著
http://www.runoob.com/react/

上一篇 下一篇

猜你喜欢

热点阅读