title: How to render value in promises on button click in react?
body: <p>I have two modules</p>
  
  <p>App.jsx</p>
  
  <pre><code>import React from 'react';
  import ReactDOM from 'react-dom';
  import {accounts} from './contract.jsx';
  
  
  class App extends React.Component{
      constructor(props){
      super(props);
      this.state={'text':'','accounts':'','clicked':false};
  }
  componentDidMount = () =&gt; {
      this.setState({'accounts':accounts()});
  }
  buttonAction = (e) =&gt; {
      this.setState({'clicked':true});
  }
  render(){
      return(
      &lt;div align="center"&gt;
      &lt;Button name="get all Accounts" action={this.buttonAction}/&gt;
      {this.state.clicked ? &lt;div&gt;{this.state.accounts}&lt;/div&gt; : null}
      &lt;/div&gt;
      )}
  }
  
  
  const Button = (props) =&gt;
      (&lt;button onClick={props.action}&gt;{props.name}&lt;/button&gt;);
  
  ReactDOM.render(&lt;App/&gt;,document.getElementById('root'));
  </code></pre>
  
  <p>and contract.jsx</p>
  
  <pre><code>import Web3 from 'web3';
  var web3 = new Web3(Web3.givenProvider || 'http://localhost:8545');
  
  let accounts = () =&gt; {
      // this is a promise
      web3.eth.getAccounts().then(function(result){console.log(result);})
      .catch(function(error){console.log(error.message)})
  }
  export {accounts};
  </code></pre>
  
  <p>I'm exporting <code>accounts</code>(a promise) function from <code>contract.jsx</code> to <code>app.jsx</code>. Since I can't return value from a promise, I need to assign the value to the state of <code>App</code> component inside the promise (check <code>componentDidMount</code> ). For that, I need to replace 'console.log(result)' in <code>accounts</code> function to 'this.setState({'accounts':result})'. But the component and <code>accounts</code> are in different modules and supposed to be independent. I cannot set the state of the <code>App</code> component inside my <code>accounts</code> function. </p>
  
  <p>How can I assign the value from the promise to my state inside <code>App</code> component? Is there any other better way to do it? I could also use some code corrections to make my component work better. </p>