Skip to content

Latest commit

 

History

History
102 lines (82 loc) · 1.69 KB

react1-state.md

File metadata and controls

102 lines (82 loc) · 1.69 KB

REACT SCALE 1 - Input // Output Box

Pre-requisites: Javascript Scale 1 (doesn't exist // pre-course material covered this)
  1. Create a new react app
bun create vite
  • Choose React and Typescript
  1. Gut the App.tsx
import { useState } from 'react'

function App() {

  return (
    <>

    </>
  )
}

export default App
  1. Create an input
function App() {
  return (
    <>
      <input type="text" />
    </>
  )
}
  1. Create a button
function App() {

  return (
    <>
      <input type="text" />
      <button type="submit">Submit</button>
    </>
  )
}
  1. CONTROL the input
function App() {

  const [input, setInput] = useState('')

  return (
    <>
      <input type="text" value={input} onChange={(e) => setInput(e.target.value)} />
      <button type="submit">Submit</button>
    </>
  )
}
  1. When the user clicks the button, put the INPUT into an OUTPUT box.
function App() {

  const [input, setInput] = useState('')
  const [output, setOutput] = useState('')

  return (
    <>
      <input type="text" value={input} onChange={(e) => setInput(e.target.value)} />
      <button type="submit" onClick={() => setOutput(input)}>Submit</button>
      <div>{output}</div>
    </>
  )
}
  1. whenever the output changes, console.log "new output!"
function App() {

  const [input, setInput] = useState('')
  const [output, setOutput] = useState('')

  useEffect(() => {
    console.log("new output!", output)
  }, [output])

  return (
    <>
      <input type="text" value={input} onChange={(e) => setInput(e.target.value)} />
      <button type="submit" onClick={() => setOutput(input)}>Submit</button>
      <div>{output}</div>
    </>
  )
}