Upgrade to Pro — share decks privately, control downloads, hide ads and more …

誰でもわかる!React入門

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for azurata azurata
November 24, 2022

 誰でもわかる!React入門

先輩に「わかった気になれた」と評されたLTです。

Avatar for azurata

azurata

November 24, 2022
Tweet

More Decks by azurata

Other Decks in Technology

Transcript

  1. ͪΐͬͱ಺༰૿΍ͯ͠ΈΔ <div> <input type="text" value=“hello!”> <p></p> <p></p> </div> <script> const

    input = document.getElementsByTagName("input")[0] const p1 = document.getElementsByTagName("p")[0] const p2 = document.getElementsByTagName("p")[1] function inputHandler() { p1.innerHTML = input.value p2.innerHTML = input.value.toUpperCase() } input.addEventListener("input", inputHandler) inputHandler() </script>
  2. DOM͔ΒͦΕͧΕཁૉΛ͖࣋ͬͯͯ <div> <input type="text" value=“hello!”> <p></p> <p></p> </div> <script> const

    input = document.getElementsByTagName("input")[0] const p1 = document.getElementsByTagName("p")[0] const p2 = document.getElementsByTagName("p")[1] function inputHandler() { p1.innerHTML = input.value p2.innerHTML = input.value.toUpperCase() } input.addEventListener("input", inputHandler) inputHandler() </script>
  3. Πϕϯτϋϯυϥ࡞Δ <div> <input type="text" value=“hello!”> <p></p> <p></p> </div> <script> const

    input = document.getElementsByTagName("input")[0] const p1 = document.getElementsByTagName("p")[0] const p2 = document.getElementsByTagName("p")[1] function inputHandler() { p1.innerHTML = input.value p2.innerHTML = input.value.toUpperCase() } input.addEventListener("input", inputHandler) inputHandler() </script>
  4. جຊܗ import React from "react" function App() { return( <>

    <h1>Hello, React!</h1> </> ) } export default App
  5. TSͳΒܕ΋໌ࣔ (͜Ε͸ͳͯ͘΋ಈ͔͘΋) import React from "react" const App = ():

    JSX.Element => { return( <> <h1>Hello, React!</h1> </> ) } export default App
  6. ଞͷίϯϙʔωϯτ΋࢖ͬͯΈΔ import React from "react" import { OtherComponent } from

    "./OtherComponent.jsx" const App = (): JSX.Element => { return( <> <h1>Hello, React!</h1> <OtherComponent /> </> ) } export default App
  7. useStateͷྫ import React, { useState } from 'react'; function Example()

    { const [count, setCount] = useState(0); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}> {/* ΫϦοΫ͢Δ͝ͱʹ1૿͑Δ */} Click me </button> </div> ); }
  8. useE ff ect • ෭࡞༻ϑοΫͱ΋ݺ͹ΕΔ • ୈ2Ҿ਺͸ґଘ͢Δ஋(͍͍ͨͯState)ͷ഑ྻΛ౉͢ • ୈ2Ҿ਺ͷதʹ͋Δ஋͕มߋ͞ΕΔ౓ʹ࣮ߦ͞ΕΔ •

    ۭ഑ྻΛ౉͢͜ͱͰ࠷ॳͷҰ౓ͷΈ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δ • ٯʹԿ΋͚ͭͳ͍ͱશͯͷ஋͕ߋ৽͞ΕΔ౓ʹ࣮ߦ͞ΕΔ
  9. useE ff ectͷྫ import React, { useState, useEffect } from

    'react'; function Example() { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; // ࠶Ϩϯμʔ͞ΕΔ౓ʹݺ͹ΕΔ }); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); }
  10. useE ff ectͷྫ import React, { useState, useEffect } from

    'react'; function Example() { const [count, setCount] = useState(0); useEffect(() => { document.title = `You clicked ${count} times`; // Ұ౓͔͠ݺ͹Εͳ͍ }, []); return ( <div> <p>You clicked {count} times</p> <button onClick={() => setCount(count + 1)}>Click me</button> </div> ); }