Skip to content

React #21

Description

@phoebeCodeSpace

React 中 setState({key:value})时当key是一个变量时,这个语句要如何写。

this.setState({
    [key]:obj.xxx
})

React-router

  1. historyAPI 
  2. window.addEventListener('hashchange', function() { });
  3. https://stackoverflow.com/questions/3522090/event-when-window-location-href-changes

性能:

  • 不要在componentWillUpdate/componentDidUpdate/render中执行setState, 可能异致死循环。尽量不要在生命周期钩子外的方法中使用setState。
  • 不要在JSX中使用bind方法绑定组件实例
  • 不要使用cloneElement,createElement,让JSX与babel帮你创建它们。
// bad
class extends React.Component {
  onClickDiv() {
    // do stuff
  }

  render() {
    return <div onClick={this.onClickDiv.bind(this)} />;
  }
}

// good
class extends React.Component {
  constructor(props) {
    super(props);

    this.onClickDiv = this.onClickDiv.bind(this);
  }

  onClickDiv() {
    // do stuff
  }

  render() {
    return <div onClick={this.onClickDiv} />;
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions