Skip to content

Instantly share code, notes, and snippets.

@KevinVR
Created February 26, 2021 11:05
Show Gist options
  • Save KevinVR/cfceb985b44a5c9deb9eeba0a5d471f5 to your computer and use it in GitHub Desktop.
Save KevinVR/cfceb985b44a5c9deb9eeba0a5d471f5 to your computer and use it in GitHub Desktop.
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