Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.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 未使用组件willmount安装的状态_Reactjs_Antd - Fatal编程技术网

Reactjs 未使用组件willmount安装的状态

Reactjs 未使用组件willmount安装的状态,reactjs,antd,Reactjs,Antd,我有一个树节点,我试图在用户选择一个节点后显示内容,但内容没有改变 constructor(props) { super(props); this.state = { t:["",""], loading: false, disPageTken:"", }; } componentWillMount(){ window.FB.api( “/我”, “得到”, {“fields”:“posts{created_time,like

我有一个树节点,我试图在用户选择一个节点后显示内容,但内容没有改变

 constructor(props) {
    super(props);

    this.state = {
      t:["",""],
      loading: false,
      disPageTken:"",
    };
  }
componentWillMount(){
window.FB.api(
“/我”,
“得到”,
{“fields”:“posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture”,“access_token”:this.state.disPageTken},
功能(响应){
console.log(响应)
如果(答复.员额){
让listData=[];
for(设i=0;i

}title=“Test Page1”key=“0”isLeaf/>
}
title=“测试页面2”
key=“1”
岛屿
/>
我不明白为什么,我尝试了componentDidMount,但我认为有些东西没有检测到更改?

这里有一些东西

您的状态没有改变,因为调用
setState
的函数的
this
值与您预期的值不同。如果检查控制台,您应该会看到一个错误,表明
this.setState
不是一个函数。您需要更改以下内容:

window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
function(response) {
  console.log(response)
为此:

window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
(response) => {
  console.log(response)
箭头函数捕获此
的当前值,因此此代码将按预期工作

其次,不要使用
componentWillMount()
来处理副作用(如抓取)——理想情况下,根本不要使用它。当安装组件时,可能会多次调用它,这将导致多次获取。它在较新版本的React中不存在(重命名为
UNSAFE\u componentWillMount())

相反,请使用
componentDidMount()
。当安装组件时,它只被调用一次。

这里有一些东西

您的状态没有改变,因为调用
setState
的函数的
this
值与您预期的值不同。如果检查控制台,您应该会看到一个错误,表明
this.setState
不是一个函数。您需要更改以下内容:

window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
function(response) {
  console.log(response)
为此:

window.FB.api('/me', 'GET', {"fields":"posts{created_time,likes,comments,picture,full_picture,shares,message},name,picture","access_token":this.state.disPageTken},
(response) => {
  console.log(response)
箭头函数捕获此的当前值,因此此代码将按预期工作

其次,不要使用
componentWillMount()
来处理副作用(如抓取)——理想情况下,根本不要使用它。当安装组件时,可能会多次调用它,这将导致多次获取。它在较新版本的React中不存在(重命名为
UNSAFE\u componentWillMount())

相反,请使用
componentDidMount()
。当安装组件时,只调用一次