简体   繁体   English

在获取数据之前,Console.log()显示未定义

[英]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 CONSOLE.LOG()

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.

相关问题 Console.log 未定义 - Console.log is undefined 具有分配给函数的键的对象返回未定义,但console.log显示数据 - Object with key assigned to a function returning undefined but console.log shows the data JavaScript-Console.log返回一些内容,但返回时显示未定义 - JavaScript - Console.log return something but with return it shows undefined console.log 显示该道具具有属性名称和编号,但它们未定义 - console.log shows that the prop has attributes name and number, but they are undefined Codeigniter和AJAX console.log(结果)显示未定义 - Codeigniter and AJAX console.log(result) shows undefined 在Ionic3中订阅提供程序后,Console.log显示为未定义 - Console.log shows as undefined after subscribing to provider in Ionic3 React state 未定义,即使 console.log 显示它 - React state is undefined , even though console.log shows it 变量是未定义的错误(即使console.log显示变量) - Variable is undefined error (even if console.log shows variable) JS 说 object 是未定义的,即使它显示在 console.log 中 - JS says an object is undefined even though it shows with console.log 为什么我console.log一个对象,它显示对象,但是当我console.log时,它显示未定义 - Why I console.log a Object,it shows object,but when I console Object.value ,it shows undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM