首页 > 解决方案 > React.js:将道具传递给 MobX 商店

问题描述

我正在尝试将 App.js 中的 props 对象传递到 MobX 商店,如下所示:

//ui_store.js
import {message_construction_logic, App} from "../App";

export class UiStore {
    starting_array = [<ChoosePostType ui_store={ui_store} go_to_anchor_link={this.props.go_to_anchor_link} />];
...

在 App.js 中,我设置了要作为道具传递的函数,如下所示:

class App extends Component {
    constructor(props) {
        super(props);

        this.go_to_anchor_link = this.go_to_anchor_link.bind(this)
    };

    // METHODS ===========================================


    go_to_anchor_link = (anchor_id) => {

        const scroll_target = document.getElementById(anchor_id);
        console.log('anchor_id', anchor_id); // returns "question_leads", which is correct

        if (scroll_target) {
            console.log('scroll_target', scroll_target);
            smoothScroll(scroll_target);
        }
    };
...

尝试接收道具的子组件抛出未定义的错误。我没有正确导出道具吗?

标签: reactjsmobxmobx-react

解决方案


推荐阅读