小编201*_*014的帖子

为什么React会同时保留componentWillReceiveProps和shouldComponentUpdate方法?

当我使用react时,我发现这两个生命周期太相似了,componentWillReceiveProps接收nextProps作为参数,shouldComponentUpdate接收nextProps和nextState作为参数,所以我认为shouldComponentUpdate可以做同样的事情以及更多,为什么react保持componentWillReceiveProps方法,我想知道是什么这两种方法之间的区别

javascript reactjs virtual-dom

5
推荐指数
2
解决办法
2570
查看次数

React 键如何工作?

import React from 'react'
import ReactDOM from 'react-dom'

class App extends React.Component{
	  constructor(props) {
		    super(props)
		    this.state = {
			      list: [{id: 1,val: 'aa'}, {id: 2, val: 'bb'}, {id: 3, val: 'cc'}]
		    }
	  }

	  click() {
		    this.state.list.reverse()
		    this.setState({})
	  }

	  render() {
		    return (
            <ul>
                <div onClick={this.click.bind(this)}>reverse</div>
                {
                	this.state.list.map(function(item, index) {
                		return (
                            <Li key={item.id} val={item.val}></Li>
                		)
                	}.bind(this))
                }
            </ul>
		    )
	  }
}

class Li extends React.Component{
	  constructor(props) {
		    super(props)
	  }
	  componentDidMount() {
		    console.log('===did===')
	  }
	  componentWillUpdate(nextProps, nextState) {
		    console.log('===mount====')
	  }
	  render() {
		    return …
Run Code Online (Sandbox Code Playgroud)

html javascript reactjs virtual-dom

1
推荐指数
1
解决办法
2477
查看次数

标签 统计

javascript ×2

reactjs ×2

virtual-dom ×2

html ×1