Skip to content

Instantly share code, notes, and snippets.

@berkelmas
Created July 25, 2020 17:34
Show Gist options
  • Save berkelmas/2738f64ee1885ed65fdeff78d1c372d3 to your computer and use it in GitHub Desktop.
Save berkelmas/2738f64ee1885ed65fdeff78d1c372d3 to your computer and use it in GitHub Desktop.
import React, {useState} from "react";
const ExampleComponent = props => {
const [exampleState, setExampleState] = useState({name: "Berk"});
const changeName = () => {
// setExampleState will provide us with changing state;
// setExampleState will also provide previous state if we want to use it.
setExampleState(prev => ({name: "Changed..."}));
}
return (
<>
{/* exampleState will give us {name: "Berk"} */
<div>{exampleState.name}</div>
<button onClick={changeName}>Change Name</button>
</>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment