Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何设置状态,继续获取TypeError:无法读取未定义的属性“setState”_Reactjs_Setstate - Fatal编程技术网

Reactjs 如何设置状态,继续获取TypeError:无法读取未定义的属性“setState”

Reactjs 如何设置状态,继续获取TypeError:无法读取未定义的属性“setState”,reactjs,setstate,Reactjs,Setstate,我正试图用从axios get返回的数据设置状态。但我一直收到以下错误类型错误:无法读取undefined的属性“setState” 类页扩展了React.Component{ constructor(props) { super(props); this.authenticated = props.authenticated; //this.handleClick = this.handleClick.bind(this); } state ={ page

我正试图用从axios get返回的数据设置状态。但我一直收到以下错误类型错误:无法读取undefined的属性“setState”

类页扩展了React.Component{

constructor(props) {
    super(props);
    this.authenticated = props.authenticated;
    //this.handleClick = this.handleClick.bind(this);
} 

state ={
    page : []
}

componentDidMount() {
    let config = null;
    //let token = null;
    app.auth().currentUser.getIdToken(true).then(function(idToken) {
        config = {
            headers: {'Authorization': idToken}
        };
        console.log(config);
        axios.get('http://localhost:4001/api/v1/page',config)
        .then(res => {
            console.log(res.data);
          const page = res.data;
          this.setState( page );
        }).catch((err)=>{
            console.log(err);
        });

     })

  }

render () {

    const authenticated = this.authenticated;
    console.log(authenticated);
    console.log(this.state);

    return (
        <h1> test 123</h1>
      );
}
}

axios内部指的是不同的东西,将其值存储在变量中并使用该变量

componentDidMount = () => {
  let config = null;
  //let token = null;
  const current = this; // here save this in current variable
  app
    .auth()
    .currentUser.getIdToken(true)
    .then(function(idToken) {
      config = {
        headers: { Authorization: idToken }
      };
      console.log(config);
      axios
        .get('http://localhost:4001/api/v1/page', config)
        .then(res => {
          console.log(res.data);
          const page = res.data;
          current.setState(page); // use current here
        })
        .catch(err => {
          console.log(err);
        });
    });
}

谢谢你,伙计,你是一个传奇。希望你圣诞节得到你想要的一切