Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

post method in reactjs hooks.

export default function App() {
  const [data, setData] = useState([])
  const [formData, setFormData] = useState('')

  useEffect(() => {
    fetchGames() // Fetch games when component is mounted
  }, [])

  const fetchGames = () => {
    fetch('http://localhost:3000/game', {
      method: 'GET',
    })
      .then((res) => res.json())
      .then((result) => setData(result.rows))
      .catch((err) => console.log('error'))
  }

  const saveGames = () => {
    fetch('http://localhost:3000/game', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        name: formData, // Use your own property name / key
      }),
    })
      .then((res) => res.json())
      .then((result) => setData(result.rows))
      .catch((err) => console.log('error'))
  }

  const handleSubmit = (event) => {
    event.preventDefault()
    saveGames() // Save games when form is submitted
  }

  const handleChange = (event) => {
    setFormData(event.target.value)
  }

  return (
    <div className="App">
      {/* method="post" not needed here because `fetch` is doing the POST not the `form` */}
      {/* Also, note I changed the function name, handleSubmit */}
      <form onSubmit={handleSubmit}>
        <input type="text" name="name" value={formData} onChange={handleChange} />
        <button type="submit">click</button>
      </form>

      {data &&
        data.map((element, index) => (
          <GameTestResult name={element.name} key={element.index} />
        ))}
    </div>
  )
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: access shadow root element 
Javascript :: Destructuring of object in ES6 
Javascript :: destructuring in es6 
Javascript :: js objects 
Javascript :: check null or undefined in javascript 
Javascript :: onclick increase counter javascript 
Javascript :: jsx example 
Javascript :: javascript add query string to url 
Javascript :: repeat js 
Javascript :: afficher une variable dans la console javascript 
Javascript :: jquery select direct child 
Javascript :: every in javascript 
Javascript :: form contact 7 ajax send 
Javascript :: angular 11 features 
Javascript :: jsx return greatest number between two numbers 
Javascript :: nuxt auth user info 
Javascript :: promise syntax for javascript 
Javascript :: moment.js format 
Javascript :: how to export fs.readFile 
Javascript :: javascript prevent value change in select option 
Javascript :: return object from map javascript 
Javascript :: @input in angular 
Javascript :: addAndRemoveClassJquery 
Javascript :: floor javascript 
Javascript :: installing babel from command line 
Javascript :: check if computer online js 
Javascript :: sumar un mes a una fecha javascript moment 
Javascript :: angular dropdown selected value 
Javascript :: electron iframe require is not defined 
Javascript :: sendmediagroup telegram nodejs 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =