leecade / leecade/react-tips

生命周期 Lifecycle

Open
#6 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Instantiation

  1. getDefaultProps
  2. getInitialState
  3. componentWillMount
  4. render
  5. componentDidMount

componentDidMount 不会在服务端被渲染的过程中调用

setState

  1. shouldComponentUpdate
  2. componentWillUpdate
  3. render
  4. componentDidUpdate

Mount

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

阻止不必要的 render, 优化性能, 可以 Mixin PureRenderMixin


shouldComponentUpdate (nextProps, nextState) {
    return this.state.checked === nextState.checked; // return false 则不更新组件

    // 确定该组件渲染完后无需再次更新,即这个组件是一个静态组件,那么可以直接return false(优化性能)
    // return false
}

反模式, this.props 计算后的值不应该赋给 state, 否则会导致计算后的值永远不会与派生出它的 props 值不同步

static defaultProps = {
  data: new Date()
}

static state = {
  data: this.props.date - new Date()
}

render () {
    return <div>Day:{this.state.day}</div>
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is identified. Start by reading the lifecycle notes and JSX examples in the issue; the issue does not state a specific documentation change or a clear completion condition.

Written by the indexing model from the issue text.

Assessment

Tech stack
react
Domain
documentation
Issue type
Documentation
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.