尝试触发Mobx操作会导致Uncaught Type Error

时间:2018-05-26 15:48:49

标签: reactjs mobx

我正在尝试触发Mobx操作,并且它正在生成错误“Uncaught TypeError:无法读取未定义的属性'arrowVisible'。”无法弄清楚为什么(函数由react-waypoint触发):

codesandbox.io复制在这里)

import React from "react";
import PropTypes from "prop-types";
import { Container, Row, Col } from "reactstrap";
import { observer, inject } from "mobx-react";
import Waypoint from "react-waypoint";

@inject("styleStore")
@observer
export class Questions extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      entered: false
    };
  }
  componentDidMount() {
    window.scrollTo(0, 0);
  }

  setWaypointEnter() {
    // Set waypoint state
    this.setState({ entered: true });
    // Use updated state for UI waypoints animation.
    if (this.state.entered) {
      this.props.styleStore.isArrowVisible("yes");
    }
  }

  render() {
    return (
        <QuestionsDiv id="questions">
          <Container>
            <Waypoint
              scrollableAncestor={window}
              onEnter={this.setWaypointEnter.bind(this)}
              fireOnRapidScroll={true}
              topOffset="40%"
            />
...

这是MobX商店:

import { observable, action } from "mobx";

class StyleStore {
  @observable nextLink;
  @observable arrowVisibile;

  @action
    isArrowVisibile(visibility) {
    this.arrowVisibile = visibility;
    console.log(this.arrowVisibile);
  }
}

export default StyleStore;

并且index.js加载提供者:

import App from "./App";
import { BrowserRouter } from "react-router-dom";
import { Provider } from "mobx-react";
import StyleStore from "./stores/styleStore";
import registerServiceWorker from "./registerServiceWorker";

const stores = {
  styleStore: new StyleStore()
};

ReactDOM.render(
  <BrowserRouter>
    <Provider {...stores}>
      <App />
    </Provider>
  </BrowserRouter>,

  document.getElementById("root")
);
registerServiceWorker();

我也在使用react-app-rewire-mobx。所以.babelrc看起来像这样:

{
  "presets": ["mobx"]
}

我在这里错过了什么会出现这个错误?另一个MobX操作ChangeNextLink按编码工作。

1 个答案:

答案 0 :(得分:1)

问题似乎在这里

 this.props.styleStore.arrowVisible();

arrowVisible是一个可观察的属性,而不是一个动作,你可能打算调用

 this.props.styleStore.isArrowVisible();

此外,您的商店还存在拼写错误

@action
    isArrowVisibile(visibility)

应该是

@action
    isArrowVisible(visibility)