Skip to content

Instantly share code, notes, and snippets.

@joaocarvalhowd
Created April 9, 2019 13:16
Show Gist options
  • Save joaocarvalhowd/e08aefa370ea33505cf82b544d92fdda to your computer and use it in GitHub Desktop.
Save joaocarvalhowd/e08aefa370ea33505cf82b544d92fdda to your computer and use it in GitHub Desktop.
React - example form with state
import React, { useReducer } from "react";
import {
AppBar, Typography, Toolbar, TextField
} from "@material-ui/core";
function App() {
const [fields, setFields] = useReducer(
(state, newState) => ({ state, ...newState }),
{ name: '', github: '', linkedin: '', website: '' }
);
const handleOnChange = e => {
const { name, value } = e.target;
setFields({ [name]: value });
};
return (
<>
<AppBar position="static" color="default">
<Toolbar>
<Typography variant="h6" color="inherit">
Performance Form
</Typography>
</Toolbar>
</AppBar>
<form>
<TextField
value={fields.name}
label="Name:"
fullWidth
onChange={handleOnChange}
/>
<TextField
value={fields.github}
label="Github:"
fullWidth
onChange={handleOnChange}
/>
<TextField
value={fields.linkedin}
label="Linkedin:"
fullWidth
onChange={handleOnChange}
/>
<TextField
value={fields.website}
label="Website:"
fullWidth
onChange={handleOnChange}
/>
</form>
</>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment