Created
April 6, 2021 11:35
-
-
Save Ruslan858/2b1f5bb256a8b54090768a22b7077e6c to your computer and use it in GitHub Desktop.
Revisions
-
Ruslan858 created this gist
Apr 6, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ App. use localStorage --------------------- A [Pen](https://codepen.io/Rubik/pen/VwPzbdo) by [Ruslan](https://codepen.io/Rubik) on [CodePen](https://codepen.io). [License](https://codepen.io/Rubik/pen/VwPzbdo/license). This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ <div id="app"></div> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,118 @@ const useState = React.useState const useEffect = React.useEffect function OurApp() { const [pets, setPets] = useState([]) // only run once the first time this component is rendered useEffect(() => { if (localStorage.getItem("examplePetData")) { setPets(JSON.parse(localStorage.getItem("examplePetData"))) } }, []) // run every time our pet state changes useEffect(() => { localStorage.setItem("examplePetData", JSON.stringify(pets)) }, [pets]) return ( <> <OurHeader /> <LikeArea /> <TimeArea /> <AddPetForm setPets={setPets} /> <ul> {pets.map(pet => <Pet setPets={setPets} id={pet.id} name={pet.name} species={pet.species} age={pet.age} key={pet.id} />)} </ul> <Footer /> </> ) } function AddPetForm(props) { const [name, setName] = useState() const [species, setSpecies] = useState() const [age, setAge] = useState() function handleSubmit(e) { e.preventDefault() props.setPets(prev => prev.concat({name, species, age, id: Date.now()})) setName("") setSpecies("") setAge("") } return ( <form onSubmit={handleSubmit}> <fieldset> <legend>Add New Pet</legend> <input value={name} onChange={e => setName(e.target.value)} placeholder="Name" /> <input value={species} onChange={e => setSpecies(e.target.value)} placeholder="species" /> <input value={age} onChange={e => setAge(e.target.value)} placeholder="age in years" /> <button>Add Pet</button> </fieldset> </form> ) } function LikeArea() { const [likeCount, setLikeCount] = useState(0) function increaseLikeHandler() { setLikeCount(function(prev) { return prev + 1 }) } function decreaseLikeHandler() { setLikeCount(prev => { if (prev > 0) { return prev - 1 } return 0 }) } return ( <> <button onClick={increaseLikeHandler}>Increase likes</button> <button onClick={decreaseLikeHandler}>Decrease likes</button> <h2>This page has been liked {likeCount} times.</h2> </> ) } function Pet(props) { function handleDelete() { props.setPets(prev => prev.filter(pet => pet.id != props.id)) } return ( <li>{props.name} is a {props.species} and is {props.age} years old. <button onClick={handleDelete}>Delete</button> </li> ) } function Footer() { return <small>Copyright Footer Text</small> } function TimeArea() { const [theTime, setTheTime] = useState(new Date().toLocaleString()) useEffect(() => { const interval = setInterval(() => setTheTime(new Date().toLocaleString()), 1000) return () => clearInterval(interval) }, []) return <p>The current time is {theTime}.</p> } function OurHeader() { return <h1 className="special">Our Amazing App Header</h1> } ReactDOM.render(<OurApp />, document.querySelector("#app")) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,2 @@ <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.12.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script> This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ .special { color: orange; }