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?)

TechnoCrat
  • 710
  • 10
  • 20
Tyler L
  • 835
  • 2
  • 16
  • 28

1 Answers1

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?

Hugo Silva
  • 6,748
  • 3
  • 25
  • 42
  • 1
    It will work for `{_root:entries:[ {place_id: 'TRYING TO GET THIS VALUE"`. OP has different result. – Manwal Sep 07 '17 at 05:22
  • This is better than using the index of the array. Thank you :) And thanks for the answer Hugo. – Tyler L Sep 07 '17 at 06:04