Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在状态更改时调用渲染,需要在子级上更新道具,但子级不会重新渲染_Javascript_Reactjs_Components_Rendering_Web3 - Fatal编程技术网

Javascript 在状态更改时调用渲染,需要在子级上更新道具,但子级不会重新渲染

Javascript 在状态更改时调用渲染,需要在子级上更新道具,但子级不会重新渲染,javascript,reactjs,components,rendering,web3,Javascript,Reactjs,Components,Rendering,Web3,我有以下代码: // Packages import React, { Component } from 'react'; import Web3 from 'web3'; import Header from './components/Header'; class App extends Component { constructor(props) { super(props); this.state = { account: null, con

我有以下代码:

// Packages
import React, { Component } from 'react';
import Web3 from 'web3';
import Header from './components/Header';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      account: null,
      contract: null,
    }

  }

  componentDidMount = () => {
    if (typeof window.web3 !== 'undefined') {
        this.state.web3.eth.getAccounts((err, accounts) => {
          if(err)
              throw err;
          } else {
            this.state.web3.eth.defaultAccount = accounts[0];
            this.contract = new this.state.web3.eth.Contract(abi.abi, this.contractAddress);
            this.setState({
              account : accounts[0],
              contract : this.contract
            });

          }
      });
    } else {
        console.log('No web3? You should consider trying MetaMask!')
    }
  }

  render() {
    console.log(this.state);
    return (

      <div className="App">
        <Header
          account = {this.state.account}
          web3={this.state.web3}
          contract={this.state.contract}
        />          </div>
    );
  }
}

export default App;

我完全不知道会出什么问题

在您的情况下,componentWillReceiveProps()是处理更新的prop值的合适生命周期方法。您可以在此处阅读:

componentDidMount
将只调用一次。尝试登录
render
。可以,谢谢!是否有重新招标的活动?我想访问我更新的道具,而不会影响渲染功能。
componentdiddupdate
等。在这里查看所有生命周期真棒,非常感谢。。我只是在错误的地方搜索,这将很快被弃用,应该改用
getDerivedStateFromProps()
...
    class Header extends React.Component {
      constructor(props) {
        super(props);
        this.state = {
          accountBalance: "-",
          formattedAddress: '0x0...000',
          account: '0x0'
        }

      }

      componentDidMount(){
        console.log('rendered');
      }
...