Skip to content

Instantly share code, notes, and snippets.

@joaocarvalhowd
Created April 9, 2019 13:40
Show Gist options
  • Save joaocarvalhowd/86fa6840b7e6115b072a0c36ac106763 to your computer and use it in GitHub Desktop.
Save joaocarvalhowd/86fa6840b7e6115b072a0c36ac106763 to your computer and use it in GitHub Desktop.
More easily
import React, { useRef } from "react";
import {
AppBar, Typography, Toolbar, TextField
} from "@material-ui/core";
function App() {
const fields = {
name: useRef(),
github: useRef(),
linkedin: useRef(),
website: useRef()
};
const handleSubmit = e => {
e.preventDefault();
const data = Object.keys(fields).reduce(
(acc, field) => ({ ...acc, [field]: field[field].current.value }),
{}
);
console.log(data);
};
return (
<>
<AppBar position="static" color="default">
<Toolbar>
<Typography variant="h6" color="inherit">
Performance Form
</Typography>
</Toolbar>
</AppBar>
<form onSubmit={handleSubmit}>
<TextField
ref={fields.name}
label="Name:"
fullWidth
/>
<TextField
ref={fields.github}
label="Github:"
fullWidth
/>
<TextField
ref={fields.linkedin}
label="Linkedin:"
fullWidth
/>
<TextField
ref={fields.website}
label="Website:"
fullWidth
/>
</form>
</>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment