Created
February 26, 2021 11:05
-
-
Save KevinVR/cfceb985b44a5c9deb9eeba0a5d471f5 to your computer and use it in GitHub Desktop.
This file contains 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 characters
import React, {useState} from 'react'; | |
function MyStateComponent = ({ showHeading }) => { | |
// Also note, the value we pass to the useState method | |
// will be the default/initial value! | |
const [headingText, setHeadingText] = useState('Default Heading'); | |
// Our second state variable | |
const [paragraphText, setParagraphText] = useState('Default Paragraph'); | |
// Now, update the state depending on some condition! | |
if (showHeading) { | |
setHeadingText('Show Heading is True!'); | |
} | |
// get a random number between 0 - 10 | |
const randomNumber = Math.floor(Math.random() * 11); | |
setParagraphText(`The random number is: ${randomNumber}`); | |
// Let's keep the heading in a variable to keep it readable | |
const heading = showHeading ? <h1>{headingText}</h1> : null; | |
return <div> | |
{heading} | |
<p> | |
{paragraphText} | |
</p> | |
</div>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment