Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MobX React TypeError: Cannot read property of undefined

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.

like image 487
Shawn Avatar asked Jun 09 '26 16:06

Shawn


1 Answers

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

like image 68
Tholle Avatar answered Jun 11 '26 13:06

Tholle