[英]Console.log() shows undefined before getting data
I seem to have a lifecycle hook issue that I can't seem to solve. 我似乎有一个生命周期钩子问题,我似乎无法解决。
export default class EditRevision extends Component {
state = {
data: [],
customColumns: []
}
componentWillMount = () => {
axios.get('http://localhost:8080/lagbevakning/revision/subscriptions?id=' + (this.props.match.params.id)).then(response => {
this.setState({
data: response.data,
loading: false
})
})
}
render() {
/* THIS IS THE CONSOLE.LOG() I AM REFERRING TO */
console.log(this.state.data.subscriptionRevisionDTOS)
return (
<div></div>
)
}
}
And this is my log upon rendering the component https://i.gyazo.com/9dcf4d13b96cdd2c3527e36224df0004.png 这是我在渲染组件时的日志 https://i.gyazo.com/9dcf4d13b96cdd2c3527e36224df0004.png
It is undefined, then retrieves the data as i desire it to, then it gets undefined again. 它是未定义的,然后按照我的意愿检索数据,然后它再次未定义。
Any suggestions on what causes this issue is much appreciated, thank you. 非常感谢有关导致此问题的原因的任何建议,谢谢。
Replace this: 替换这个:
componentWillMount = () => {
axios.get('http://localhost:8080/lagbevakning/revision/subscriptions?id=' + (this.props.match.params.id)).then(response => {
this.setState({
data: response.data,
loading: false
})
})
with: 有:
constructor(props){
super(props)
this.state = {
data: [],
customColumns: []
}
axios.get('http://localhost:8080/lagbevakning/revision/subscriptions?id=' + (this.props.match.params.id)).then(response => {
this.setState({
data: response.data,
loading: false
})
})
}
try to call axios in constructor or componentDidMount() (componentWillMount should not be used). 尝试在构造函数或componentDidMount()中调用axios(不应使用componentWillMount)。 the undefined result is caused by the async call.
未定义的结果是由异步调用引起的。 Looks like you have a lot of uncontrolled renders.
看起来你有很多不受控制的渲染。 try to add a shouldComponentUpdate function or convert your component in a PureComponent
尝试添加一个shouldComponentUpdate函数或在PureComponent中转换您的组件
Take a look at https://reactjs.org/docs/react-component.html 看看https://reactjs.org/docs/react-component.html
export default class EditRevision extends Component {
state = {
data:{subscriptionRevisionDTOS:[]},
customColumns: []
}
componentDidMount = () => {
axios.get('http://localhost:8080/lagbevakning/revision/subscriptions?id=' +
(this.props.match.params.id)).then(response => {
this.setState({
data: response.data,
loading: false
})
})
render() {
/* THIS IS THE CONSOLE.LOG() I AM REFERRING TO */
console.log(this.state.data.subscriptionRevisionDTOS)
return (
<div></div>
)
}
see this it should be like this 看到这应该是这样的
You have init the state with 你有初始状态
state = {
data: [],
customColumns: []
}
Here this.state.data
is empty array which did not have definition of subscriptionRevisionDTOS that is why you are getting this.state.data.subscriptionRevisionDTOS
undefined
. 这里
this.state.data
是空数组,它没有subscriptionRevisionDTOS的定义,这就是为什么你得到this.state.data.subscriptionRevisionDTOS
undefined
。
Meanwhile, your async
axios.get call is completed and this.state.data
is updated with subscriptionRevisionDTOS
. 同时,您的
async
axios.get调用已完成, this.state.data
已使用subscriptionRevisionDTOS
更新。
As soon as state
is updated render() called again and you are getting the proper value of this.state.data.subscriptionRevisionDTOS
. 一旦
state
更新,再次调用render()并获得this.state.data.subscriptionRevisionDTOS
的正确值。
So below line will surely work. 所以线下肯定会有效。
state = {
data:{subscriptionRevisionDTOS:[]},
customColumns: []
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.