search

Home  >  Q&A  >  body text

javascript - react checks that the input box is not rendered for the first time, checks whether there is a default value, and in which life cycle it is written

Scene description

1. I have a news list, and the news list has an edit button
2. Clicking the edit button will bring up a pop-up box with default values, but some input boxes are empty
3 .The input box in the click-edit pop-up box is a separate component.

Problem Description

Because the input box in the pop-up box is a separate component, after I change one news, I will click the edit of another news, so I want to ask in which life cycle I should check each click. Whether the input box in the pop-up box has a default value and responds accordingly.

黄舟黄舟2774 days ago621

reply all(3)I'll reply

  • PHPz

    PHPz2017-05-19 10:48:20

    componentWillMount

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:48:20

    componentDidMount

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:48:20

    Since there is an input box, the value of the input box should be a state of the pop-up box. If there is only one pop-up box at a time, the pop-up box only needs to maintain one instance. So receive the incoming data in componentWillRecevieProps and set the value of the state based on the data:

    componentWillRecevieProps (nextProps) {
      this.setState({news: nextProps.news})
    }

    reply
    0
  • Cancelreply