title: ReactJs make a separate file for fetch
body: <p>Some days ago, I did a question about Laravel, but now I will make a shot with Nodejs and React.
  The main goal is:</p>
  
  <ul>
  <li>left bar with 4 selects, 
  
  <ul>
  <li>Depends on first select, the second will show some info.</li>
  <li>table with info from selects.</li>
  </ul></li>
  <li>On the right/center,
  
  <ul>
  <li>Show the google maps api.</li>
  </ul></li>
  <li>At the bottom right/center.
  
  <ul>
  <li>Panel with tabs. (info from the individual object from the table).</li>
  </ul></li>
  </ul>
  
  <p>For this, I want to create a file with fetch functions, for later use.
  Is there a way to setup a fetch function in a file and reuse it?</p>
  
  <pre><code>import React, { Component } from 'react';
  import { fetch_function } from './fetch_file';
  
  class App extends Component {
  constructor(props) {
      super(props);
      this.state = {
          Items: [],
          is_loaded: false,
      }
      url = "http://localhost:4000";
      this.fetch_function(url, this.state, "Items");
  }
  
  componentDidMount() {
  }
  
  render() {
      var { is_loaded, Items} = this.state;
  
      const options_select_items = Items.map((Item, id) =&gt; {
          return ({ value: Item.id, label: Item.Name })
      })
  
      return (
          &lt;Form&gt;
              &lt;Form.Group as={Row} controlId="formHorizontalClientes"&gt;
                  &lt;Form.Label column sm={3}&gt;
                      Cliente
                  &lt;/Form.Label&gt;
                  &lt;Col sm={9}&gt;
                      &lt;Select
                          closeMenuOnSelect={true}
                          defaultValue={[system_default[0]]}
                          isMulti
                          options={options_select_items}
                      /&gt;
                  &lt;/Col&gt;
              &lt;/Form.Group&gt;
          &lt;/Form&gt;
  
      );
  }
  }
  export default App;
  </code></pre>
  
  <p>And this is the fetch_file</p>
  
  <pre><code>const fetch_function = (url, setState, param) =&gt; {
      fetch(url)
          .then(response =&gt; {
              if (!response.ok) {
                  throw Error("Network failure")
              }
              return response;
          })
          .then(res =&gt; res.json())
          .then(data =&gt; {
              setState({
                  [param]: data
              })
          })
          ;
  }
  
  module.exports.fetch_function = fetch_function ;
  </code></pre>