title: React Hooks: How to make a POST request to server
body: <p>I am a beginner.I am trying to implement a POST request from React.js in a simple form, but I cannot figure out how to send POST request to database. I guess I need <code>&lt;form action=&quot;URL&quot;&gt;</code> as well.
  Any help will be appreciated.
  Below is the code from React.js(frontend)</p>
  <pre><code>import GameTestResult from './GameTestResult';
  
  export default function App() {
   const[data, setData] = useState([]);
   const [formData, setFormData] = useState(&quot;&quot;);
  
   useEffect (() =&gt; {
    fetch('http://localhost:3000/game')
    .then(res =&gt; res.json()) 
   .then(result =&gt; setData(result.rows))
    .catch(err =&gt; console.log(&quot;error&quot;))
    },[]);
  
    const handleChange = event =&gt; {
      setFormData(event.target.value)
      }
      
      const eventHandler = event =&gt; {
        event.preventDefault();
        setFormData(&quot;&quot;);
      }
  
    return (
      &lt;div className=&quot;App&quot;&gt;
          &lt;form  method=&quot;post&quot; onSubmit = {eventHandler}&gt;
            &lt;input value = {formData} onChange = {handleChange} /&gt;
            &lt;button type=&quot;submit&quot;&gt;click&lt;/button&gt;
          &lt;/form&gt;
        
        {data &amp;&amp; data.map((element, index)=&gt;(
              &lt;GameTestResult
                name = {element.name}
                key={element.index}
              /&gt;
        ))} 
  
        &lt;/div&gt;
    );
  }
  </code></pre>
  <p>here is the code from express.js(backend)</p>
  <pre><code>var router = express.Router();
  const pool = require(&quot;../config.js&quot;);
  var cors = require('cors');
  
  
  
  router.get(&quot;/game&quot;, cors(), (req, res) =&gt; {
    pool
      .query(&quot;SELECT * FROM game&quot;)
      .then((data) =&gt; res.json(data))
      .catch((e) =&gt; {
        res.sendStatus(404), console.log(e);
      });
  });
  
  
  
  router.post(&quot;/game&quot;, (req, res) =&gt; {
    const { name } = req.body; 
   
    pool
      .query('INSERT INTO game(name) values($1);', [name])
      .then(data =&gt; res.status(201).json(data))
      .catch(e =&gt; res.sendStatus(404));
   });
   
  
  module.exports = router;
  </code></pre>