在props的getderivedstate中未定义的setstate值,而且component将在第一次仅加载旧数据的情况下接收props

时间:2018-09-05 12:44:04

标签: javascript reactjs

在想要获得道具的价值的同时,组件中的单击选项将使props在撕破旧数据后只有数据,因此我无法采用新值来首先渲染。因此,我更改为使用的props的getderivedstate,但是我的状态值未更新并呈现值。

  static getDerivedStateFromProps(nextProps, prevState) {
    console.log("1",nextProps, prevState)
    if(nextProps !== prevState ) {
        axios({
                    method: 'post',
                    url: api.single_strategy + nextProps.match.params.name,
                    data: {
                      "home_id": "1fc77114-e391-405c-b569-5578a6d70183",
                      "climate": "cold",
                      "mode":nextProps.match.params.mode,
                    },
                    headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                     },

                      mode: 'no-cors',
                }).then((responses) => {
                    console.log("2",responses.data)
                    return {data: responses.data};
                })
    }
    else return null;
}



  componentWillReceiveProps(newprops) {
        console.log("propspskcoldsnhnciudbvh", newprops.match.params.name);
        axios({
            method: 'post',
            url: api.single_strategy + newprops.match.params.name,
            data: {
              "home_id": "1fc77114-e391-405c-b569-5578a6d70183",
              "climate": "cold",
              "mode":newprops.match.params.mode,
            },
            headers: { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
             },

              mode: 'no-cors',
        }).then((responses) => {
          this.setState({data : responses.data},()=>{
            console.log("data",responses.data)
            console.log(this.state.data.userstatus, "responsesType");  
          });

          }).catch(function (error) {
            console.log(error);
        })
    }

0 个答案:

没有答案