Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react hooks port requst

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 :: node and bash together 
Javascript :: dummy servers using nodejs 
Javascript :: pure-javascript-listen-to-input-value-change 
Javascript :: javascript set content in div without innerhtml 
Javascript :: Switching words in a string using replace 
Javascript :: Nested objects and files 
Javascript :: emacs some part of the text read only 
Javascript :: @Scheduled cron expresssion 
Javascript :: angular disabled spec.ts 
Javascript :: ameca face expression code xcode 
Javascript :: only integer allowed javascript 
Javascript :: Function Recurser / Infinit Calling 
Javascript :: password parsley 
Javascript :: single line vs multiple line comment js 
Javascript :: Sub-routes in Main route not getting static files ExpressJS 
Javascript :: New year chaos solution 
Javascript :: Javascript Ternary operator | Light/Dark Theme 
Javascript :: Use ChainLink Feed Registry 
Javascript :: Scotch.io - Create a CRUD App with Node and MongoDB 1 Getting Started 
Javascript :: responsive varient in material ui 
Javascript :: cloning an element 
Javascript :: formulaire sauvegarde local storage jquery 
Javascript :: explicitly import from lodash 
Javascript :: SH1 in react native 
Javascript :: remove package-lock.json from commit 
Javascript :: Function Returning This 
Javascript :: video link on videojs 
Javascript :: Javascript Recursion shuffle card 
Javascript :: using condition how to disable radio button in angular 
Javascript :: pass data between componets in react 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =