3

I am trying to create empty array in react-native using es6. But while accessing it I get an error data not defined.

Here is the code snippet which I am using to initialise the array.

  constructor() {
    super();

    this.state = {
      search: "",
      data: []
    }
  }

Here is the code through which I am trying to populate the array and at the same time logging it

.then((responseData) => {
        this.setState({
          data: responseData.hits.hits.map(function(search){
            return{
              name: search._source.service_name
            }
          })
        })
        console.log(data);

I think so there is a problem in initialisation of array can anybody rectify it?

2 Answers 2

6

Your initializing of the array looks fine. You just can't access data like a local scoped variable. The data array in your example is a property/attribute of your state object.

So you need to access your data array like this: this.state.data

Sign up to request clarification or add additional context in comments.

Comments

1

Try console.log(this.state.data) instead of console.log(data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.