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()
。当安装组件时,只调用一次