Skip to content

Instantly share code, notes, and snippets.

@oshell
Created November 17, 2019 20:42
Show Gist options
  • Save oshell/b47993c11b0bbac6137931095b243aea to your computer and use it in GitHub Desktop.
Save oshell/b47993c11b0bbac6137931095b243aea to your computer and use it in GitHub Desktop.
sample stateful function component with data attributes
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