最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS & MobX: TypeError: ... is not a function - but it is? - Stack Overflow

programmeradmin5浏览0评论

SidenavStore.js - defines if the Sidenav should be visible or not, default true:

const SidenavStore = types
  .model('SidenavStore', {
    isSidenavVisible: types.optional(types.boolean, true),
  })
  .actions(self => ({    
    hideSidenav: () => { self.isSidenavVisible = false; },
    showSidenav: () => { self.isSidenavVisible = true; },
  }));

export default SidenavStore;

The ErrorPage makes use of the SidenavStore to determine whether or not to show the Sidenav.

import sidenavStore from '../../../stores/SidenavStore';

class ErrorPage extends React.Component {
  ponentDidMount() {
    sidenavStore.hideSidenav();
  }

  ponentWillUnmount() {
    sidenavStore.showSidenav();
  }

  render() {
    return (
      <div>
        <h1>My Content Here</h1>
      </div>
    );
  }
}

And in App.jsx, the applicable code:

const sidebarStore = SidebarStore.create();

const App = () => (
  <BrowserRouter>
    <Provider sidebarStore={SidebarStore}>
      <Main>
        {code here}
      </Main>
    </Provider>
  </BrowserRouter>
);

export default App;

So my question is this:

In the browser, I'm getting the error TypeError: _stores_SidenavStore__WEBPACK_IMPORTED_MODULE_8__.default.hideSidenav is not a function. Yet, you can see that both hideSidenav and showSidenav exist in SidenavStore.js.

What am I referencing or doing wrong? Any guidance would be much appreciated.

SidenavStore.js - defines if the Sidenav should be visible or not, default true:

const SidenavStore = types
  .model('SidenavStore', {
    isSidenavVisible: types.optional(types.boolean, true),
  })
  .actions(self => ({    
    hideSidenav: () => { self.isSidenavVisible = false; },
    showSidenav: () => { self.isSidenavVisible = true; },
  }));

export default SidenavStore;

The ErrorPage makes use of the SidenavStore to determine whether or not to show the Sidenav.

import sidenavStore from '../../../stores/SidenavStore';

class ErrorPage extends React.Component {
  ponentDidMount() {
    sidenavStore.hideSidenav();
  }

  ponentWillUnmount() {
    sidenavStore.showSidenav();
  }

  render() {
    return (
      <div>
        <h1>My Content Here</h1>
      </div>
    );
  }
}

And in App.jsx, the applicable code:

const sidebarStore = SidebarStore.create();

const App = () => (
  <BrowserRouter>
    <Provider sidebarStore={SidebarStore}>
      <Main>
        {code here}
      </Main>
    </Provider>
  </BrowserRouter>
);

export default App;

So my question is this:

In the browser, I'm getting the error TypeError: _stores_SidenavStore__WEBPACK_IMPORTED_MODULE_8__.default.hideSidenav is not a function. Yet, you can see that both hideSidenav and showSidenav exist in SidenavStore.js.

What am I referencing or doing wrong? Any guidance would be much appreciated.

Share Improve this question edited Mar 14, 2019 at 16:40 IAspireToBeGladOS asked Mar 14, 2019 at 16:34 IAspireToBeGladOSIAspireToBeGladOS 1,4843 gold badges22 silver badges36 bronze badges 3
  • have you logged sidenavStore ? do you see hideSidenav method ? – samb102 Commented Mar 14, 2019 at 16:39
  • Logged? What do you mean? – IAspireToBeGladOS Commented Mar 14, 2019 at 16:40
  • console.log(sidenavStore) – samb102 Commented Mar 14, 2019 at 16:40
Add a ment  | 

1 Answer 1

Reset to default 5

You are currently importing the model and trying to use that. You instead want to get the instance from your Provider with inject and use that from the props.

import { observer, inject } from "mobx-react";

class ErrorPage extends React.Component {
  ponentDidMount() {
    this.props.sidenavStore.hideSidenav();
  }

  ponentWillUnmount() {
    this.props.sidenavStore.showSidenav();
  }

  render() {
    return (
      <div>
        <h1>My Content Here</h1>
      </div>
    );
  }
}

export default inject("sidebarStore")(observer(ErrorPage));
发布评论

评论列表(0)

  1. 暂无评论