IlanZiin

#041 UUID

Sep 22nd, 2022
1,076
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. App.jsx
  2.  
  3. import React, { useReducer, useState } from 'react'
  4. import Nomes from './Nomes'
  5.  
  6. function App(){
  7.  
  8.     const [nome, setNome] = useState('')
  9.     const [nomes, setNomes] = useState([])
  10.  
  11.     function salvarNome(){
  12.         setNomes(tmp => [...tmp, nome])
  13.     }
  14.  
  15.     return (
  16.         <>
  17.             <h1>React Unique ID</h1>
  18.             <hr />
  19.             <input type="text" onChange={e =>  setNome(e.target.value) } value={nome} />
  20.             <button onClick={salvarNome}>Salvar nome</button>
  21.             <Nomes nomes={nomes}></Nomes>
  22.         </>
  23.     )
  24. }
  25.  
  26. export default App
  27. ------------------------------------------------------------------------------------
  28. Nomes.jsx
  29.  
  30. import React from 'react'
  31. import { v4 as uuid } from 'uuid'
  32.  
  33. export default function Nomes({nomes}){
  34.     return (
  35.         <>
  36.             {nomes.map(nome => <p key={uuid()}>{nome}</p>)}
  37.         </>
  38.     )
  39. }
Advertisement
Add Comment
Please, Sign In to add comment