【发布时间】:2018-04-24 18:56:10
【问题描述】:
我在哪里使用a boilerplate called trufflebox's react-auth
-
getWeb在加载页面时被调用 (Link to code) - 创建
web3对象 (Link to Code) - 并在 Redux 存储中存储
web3(Link to code)
问题:当我从 Redux 存储中检索 web3 对象时,它是 undefined,很可能是因为在上述步骤 2 中尚未创建 web3。 p>
只有在设置后才能从 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
如果我再次检索 web3 而不去 Redux 商店,一切正常:
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
【问题讨论】:
-
你能解决这个问题吗?我在下面的回答是否能够帮助您指出正确的方向?请尽可能分享您在这方面的进展。
标签: javascript reactjs redux react-redux web3js