Abelyangh / react-study

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-study

  1. React component create:
export class Counter extends React.component {
	render () {
      return <h1>counter: {this.props.counter}</h1>
    }
}
  1. Component lifecycle :

Mounting(装配) 1. constructor() 2. componentWillMount() 3. render() 4. componentDidMount()

export class Computer extends React.component {
    constructor(props) {
       super(props);
       this.state = {
         name: props.name
         price: props.price
       }
    }

    componentWillMount() {
    	// you can do rest call. called before render method 
    }
	render() { 

       return <h1>{this.props.name} spent my {this.props.price}</h1>
    }

    componentDidMount() {
    	//推荐在这个地方做异步的请求数据。 让后触发重绘页面。
    }
}

Updating (更新) by changes to props or state

  1. componentWillReceiveProps()
  2. shouldComponentUpdate()
  3. componentWillUpdate()
  4. render() 
  5. componentDidUpdate() 

Unmounting

  componentWillUnmount() 触发在组件被删除的时候。 
  1. 强制更新UI。

    1. setState()
    2. forceUpdate()
  2. class properties (类属性)

    1. defaultProps
    2. displayName
  3. 实例属性

    1. props
    2. state
  4. ref 来操作dom <input ref={(input)=> this.input = input}/> ref的值是个函数

  5. props.children 和容器类组件

    class Card extends Component {
        render () {
          return (
            <div className='card'>
              <div className='card-content'>
                {this.props.children}
              </div>
            </div>
          )
        }
    }
    
  6. prop-types npm install --save prop-types

    static propTypes = {
    comment: PropTypes.object
    }
    

appreix

React small book

About


Languages

Language:JavaScript 55.1%Language:HTML 23.4%Language:CSS 21.5%