1

This function returns the redux state

this.context.store.getState()

Here's the state

Map {size: 2, _root: ArrayMapNode, __ownerID: undefined, __hash: undefined, __altered: false}

If I do this.context.store.getState()["size"] it will return 2, which is perfect. Now I want to retrieve place_id from the following. How do I do it?

{_root:
  entries:
    [place_id: 'TRYING TO GET THIS VALUE"

But if I do getState()["_root"]["entries"]["place_id"] it doesn't work (because it's within an array?)

1 Answer 1

2

entries is an array, meaning it may contain multiple objects. You need to select the object by index. So for example, if you were to get the ID of the first place, your code would look like this:

getState()._root.entries[0].place_id

Or

getState()["_root"]["entries"][0]["place_id"]

Be aware that if you try to access a property of an object that doesn't exist, your code will break. So you need to check if the property exist before accessing it - How do I check if an object has a property in JavaScript?

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

2 Comments

It will work for {_root:entries:[ {place_id: 'TRYING TO GET THIS VALUE". OP has different result.
This is better than using the index of the array. Thank you :) And thanks for the answer Hugo.

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.