Created
November 17, 2019 20:42
-
-
Save oshell/b47993c11b0bbac6137931095b243aea to your computer and use it in GitHub Desktop.
sample stateful function component with data attributes
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 characters
import React, { useState } from 'react'; | |
import './App.css'; | |
function App() { | |
const [color, changeColor] = useState('blue'); | |
function handleClick(e) { | |
changeColor(e.target.getAttribute('data-color')); | |
} | |
return ( | |
<div className="App"> | |
<button data-color='blue' onClick={handleClick}>blue</button> | |
<button data-color='green' onClick={handleClick}>green</button> | |
<button data-color='red' onClick={handleClick}>red</button> | |
<div className={`box ${color}`}> | |
Lorem Ipsum | |
</div> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment