javascript - 添加数据后从 Redux Store 获取数据

标签 javascript reactjs redux react-redux web3js

我正在使用 a boilerplate called trufflebox's react-auth哪里

  1. getWeb 在加载页面时调用 ( Link to code )
  2. 创建 web3 对象 ( Link to Code )
  3. 并将 web3 存储在 Redux store ( Link to code )

问题 当我从 Redux 存储中检索 web3 对象时,它是 undefined,很可能是因为 web3 尚未在上述步骤 2 中创建。

仅在设置后才从 Redux 存储中检索 web3 的正确方法应该是什么?

layouts/test/Test.js

import React, { Component } from 'react';
import store from '../../store';


class Test extends Component {

    componentWillMount() {
        this.setState({
            web3: store.getState().results.payload.web3Instance
        })
        this.instantiateContract()
    }

    instantiateContract() {
        console.log(this.state.web3)                             // UNDEFINED!!
    }


    render() {
        return (
            <h1>Test</h1>
        )
    }
}

export default Test

如果我在不去 Redux store 的情况下再次检索 web3 一切正常:

import React, { Component } from 'react';
import getWeb3 from '../../util/web3/getWeb3';


class Test extends Component {

    componentWillMount() {
        getWeb3
        .then(results => {
            this.setState({
                web3: results.payload.web3Instance
            })
            this.instantiateContract()
        })
    }


    instantiateContract() {
        console.log(this.state.web3)
    }


    render() {
        return (
            <h1>Test</h1>
        )
    }
}

export default Test

最佳答案

在创建商店后立即解决 promise

src/store.js

        import getWeb3 from './util/web3/getWeb3';
        import { createStore } from 'redux';

        //... prepare middlewares and other stuffs , then : create store
        const store = createStore(/*....configure it as you want..*/);

        // Just after creating store, here the engineering:

        getWeb3.then(results => {
          // Assuming you have suitable reducer
          // Assuming the reducer put the payload in state and accessible via "getState().results.payload.web3Instance"
          store.dispatch({ type: 'SAVE_WEB3', payload: results.payload.web3Instance });
        });


        export default store;

在你的 ./index.js (你正在渲染整个应用程序的地方)考虑使用 Provider 组件作为包装器来传递 store behind the seen 并有一个单例店铺。

src/index.js

        import { Provider } from 'react-redux';
        import store from './store';

        ReactDOM.render(

          <Provider store={store}>
            <Test />
          </Provider>
        )

现在,在您的组件中,connect HOC 将完成所有工作,请参阅下面的注释:

src/.../Test.js

        import { connect } from 'react-redux';


        class Test extends Component {
           // No need any lifecyle method , "connect" will do everything :)

            render() {
                console.log(this.props.web3)      
                return (
                    <h1>Test</h1>
                )
            }
        }
        // Retrieve from Redux STATE and refresh PROPS of component

        function mapStateToProps(state) {
          return {
            web3: state.results.payload.web3Instance  // since you are using "getState().results.payload.web3Instance"
          }
        }
        export default connect(mapStateToProps)(Test); // the awesome "connect" will refresh props 

关于javascript - 添加数据后从 Redux Store 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47241522/

相关文章:

javascript - Ember.js 2.4 中的更新记录

javascript - 如何防止稍后更改复选框值?

reactjs - 如何在 Nextjs 布局中使用上下文 Hook

测试 createAsyncThunk Redux Toolkit Jest

javascript - 带有引导列的 map

javascript - 使用 Jasmine 在 Karma 中进行 AngularJS 工厂测试

javascript - promise 的连接不返回任何内容 (JS)

reactjs - 如何在 react-admin ListView 上限制列宽

reactjs - react pdf重复故障

android - react-redux 调度操作不适用于 android 但适用于 ios