我正在使用一个名为trufflebox的react-auth脚手架(点击此处查看代码),其中:
问题:当我从Redux store中检索web3
对象时,它是undefined
,很可能是因为在第二步中web3
尚未被创建。
解决方案:正确的方法是只有在第2步完成后才从Redux store中检索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存储,只需再次检索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