setState() inside of componentDidUpdate()

后端 未结 6 2056
鱼传尺愫
鱼传尺愫 2020-11-28 03:23

I\'m writing a script which moves dropdown below or above input depending on height of dropdown and position of the input on the screen. Also I want to set modifier to dropd

6条回答
  •  天命终不由人
    2020-11-28 04:03

    This example will help you to understand the React Life Cycle Hooks.

    You can setState in getDerivedStateFromProps method i.e. static and trigger the method after props change in componentDidUpdate.

    In componentDidUpdate you will get 3rd param which returns from getSnapshotBeforeUpdate.

    You can check this codesandbox link

    // Child component
    class Child extends React.Component {
      // First thing called when component loaded
      constructor(props) {
        console.log("constructor");
        super(props);
        this.state = {
          value: this.props.value,
          color: "green"
        };
      }
    
      // static method
      // dont have access of 'this'
      // return object will update the state
      static getDerivedStateFromProps(props, state) {
        console.log("getDerivedStateFromProps");
        return {
          value: props.value,
          color: props.value % 2 === 0 ? "green" : "red"
        };
      }
    
      // skip render if return false
      shouldComponentUpdate(nextProps, nextState) {
        console.log("shouldComponentUpdate");
        // return nextState.color !== this.state.color;
        return true;
      }
    
      // In between before real DOM updates (pre-commit)
      // has access of 'this'
      // return object will be captured in componentDidUpdate
      getSnapshotBeforeUpdate(prevProps, prevState) {
        console.log("getSnapshotBeforeUpdate");
        return { oldValue: prevState.value };
      }
    
      // Calls after component updated
      // has access of previous state and props with snapshot
      // Can call methods here
      // setState inside this will cause infinite loop
      componentDidUpdate(prevProps, prevState, snapshot) {
        console.log("componentDidUpdate: ", prevProps, prevState, snapshot);
      }
    
      static getDerivedStateFromError(error) {
        console.log("getDerivedStateFromError");
        return { hasError: true };
      }
    
      componentDidCatch(error, info) {
        console.log("componentDidCatch: ", error, info);
      }
    
      // After component mount
      // Good place to start AJAX call and initial state
      componentDidMount() {
        console.log("componentDidMount");
        this.makeAjaxCall();
      }
    
      makeAjaxCall() {
        console.log("makeAjaxCall");
      }
    
      onClick() {
        console.log("state: ", this.state);
      }
    
      render() {
        return (
          

    Color: {this.state.color}

    ); } } // Parent component class Parent extends React.Component { constructor(props) { super(props); this.state = { value: 1 }; this.tick = () => { this.setState({ date: new Date(), value: this.state.value + 1 }); }; } componentDidMount() { setTimeout(this.tick, 2000); } render() { return (

    Parent

    ); } } function App() { return ( ); } const rootElement = document.getElementById("root"); ReactDOM.render(, rootElement);

提交回复
热议问题