3

I am trying React with MobX.

I defined fetchSources() in my store. If I put the following at the top level component it works fine.

  componentWillMount() {
    this.props.store.fetchSources() 
  } 

But if I put the above same code into child level component which is also defined as an observer it gives me the following error.

TypeError: Cannot read property 'fetchSources' of undefined.

Any idea as to how to fix it?

Update:

Tholle is correct. The problem is resolved by passing the store to the child component.

Shawn
  • 5,130
  • 13
  • 66
  • 109
  • 3
    Could you show the render method and the child component as well? It's hard to say with just the code in the question. Either you have to pass the store as a prop to the child, or you could use a [**Provider and inject**](https://github.com/mobxjs/mobx-react#provider-and-inject). – Tholle Jun 05 '17 at 07:16
  • Tholle, thanks for the response. I updated my post with code. – Shawn Jun 05 '17 at 15:44

1 Answers1

1

Make sure you pass the fetchSources as a prop to the child component as well, and it should work.

Tholle
  • 108,070
  • 19
  • 198
  • 189