I have a small app with a Form ponent, a SubmitButton ponent, and my parent (App.js) ponent. When the user clicks the submit button I want to get the values of the 3 fields on my form ponent and pass them to my App.js ponent. I am not sure how to trigger the event using onClick()
or something like it to grab the form field values from my form and then pass them via props
to the App.js ponent and console.log()
them. Can anyone provide some guidance as I am very new to React?
App.js
import React from 'react';
import NavBar from './Components/NavBar'
import Form from './Components/InfoForm'
import SubmitButton from './Components/SubmitButton';
import Container from '@material-ui/core/Container';
import Checkbox from './Components/Checkbox';
import './App.css';
function App() {
const [state, setState] = React.useState({
checkbox: false,
});
const handleChange = event => {
setState({ ...state, [event.target.name]: event.target.checked });
};
return (
<div>
<Container maxWidth="md">
<NavBar />
<Form />
<Checkbox name="checkbox" onChange={handleChange} checked={state.checkbox} />
<SubmitButton isEnabled={state.checkbox} onClick={} />
</Container>
</div>
);
}
export default App;
InfoForm.js
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import MenuItem from '@material-ui/core/MenuItem';
import TextField from '@material-ui/core/TextField';
function Form() {
const classes = useStyles();
const [values, setValues] = React.useState({
name: '',
email: '',
months: ''
});
const handleChange = name => event => {
setValues({ ...values, [name]: event.target.value });
};
return (
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-name"
label="Name"
className={classes.textField}
value={values.name}
onChange={handleChange('name')}
margin="normal"
variant="outlined"
/>
<TextField
id="outlined-email-input"
label="Email"
className={classes.textField}
value={values.email}
type="email"
name="email"
autoComplete="email"
margin="normal"
variant="outlined"
/>
<TextField
id="outlined-select-months"
select
label="Select"
className={classes.textField}
value={values.months}
onChange={handleChange('months')}
SelectProps={{
MenuProps: {
className: classes.menu,
},
}}
helperText="Month looking to book"
margin="normal"
variant="outlined"
>
{months.map(option => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
</form>
);
}
export default Form;
SubmitButton.js
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
export default function ContainedButtons(props) {
return (
<div>
<Button variant="contained" color="primary" className={classes.button} disabled = {!props.isEnabled} type="submit">
Submit
</Button>
</div>
);
}
I have a small app with a Form ponent, a SubmitButton ponent, and my parent (App.js) ponent. When the user clicks the submit button I want to get the values of the 3 fields on my form ponent and pass them to my App.js ponent. I am not sure how to trigger the event using onClick()
or something like it to grab the form field values from my form and then pass them via props
to the App.js ponent and console.log()
them. Can anyone provide some guidance as I am very new to React?
App.js
import React from 'react';
import NavBar from './Components/NavBar'
import Form from './Components/InfoForm'
import SubmitButton from './Components/SubmitButton';
import Container from '@material-ui/core/Container';
import Checkbox from './Components/Checkbox';
import './App.css';
function App() {
const [state, setState] = React.useState({
checkbox: false,
});
const handleChange = event => {
setState({ ...state, [event.target.name]: event.target.checked });
};
return (
<div>
<Container maxWidth="md">
<NavBar />
<Form />
<Checkbox name="checkbox" onChange={handleChange} checked={state.checkbox} />
<SubmitButton isEnabled={state.checkbox} onClick={} />
</Container>
</div>
);
}
export default App;
InfoForm.js
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import MenuItem from '@material-ui/core/MenuItem';
import TextField from '@material-ui/core/TextField';
function Form() {
const classes = useStyles();
const [values, setValues] = React.useState({
name: '',
email: '',
months: ''
});
const handleChange = name => event => {
setValues({ ...values, [name]: event.target.value });
};
return (
<form className={classes.container} noValidate autoComplete="off">
<TextField
id="outlined-name"
label="Name"
className={classes.textField}
value={values.name}
onChange={handleChange('name')}
margin="normal"
variant="outlined"
/>
<TextField
id="outlined-email-input"
label="Email"
className={classes.textField}
value={values.email}
type="email"
name="email"
autoComplete="email"
margin="normal"
variant="outlined"
/>
<TextField
id="outlined-select-months"
select
label="Select"
className={classes.textField}
value={values.months}
onChange={handleChange('months')}
SelectProps={{
MenuProps: {
className: classes.menu,
},
}}
helperText="Month looking to book"
margin="normal"
variant="outlined"
>
{months.map(option => (
<MenuItem key={option.value} value={option.value}>
{option.label}
</MenuItem>
))}
</TextField>
</form>
);
}
export default Form;
SubmitButton.js
import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import Button from '@material-ui/core/Button';
export default function ContainedButtons(props) {
return (
<div>
<Button variant="contained" color="primary" className={classes.button} disabled = {!props.isEnabled} type="submit">
Submit
</Button>
</div>
);
}
Share
Improve this question
edited Jun 18, 2019 at 3:22
Dean Friedland
asked Jun 18, 2019 at 3:01
Dean FriedlandDean Friedland
8033 gold badges15 silver badges36 bronze badges
1
- how are you using this button? – Junius L Commented Jun 18, 2019 at 3:25
1 Answer
Reset to default 2If you don't want to move your ponents you can take the state handler out of infoForm.js
move this code to app.js and change the handler name to "handleChangeForm" (optional name)
const [values, setValues] = React.useState({
name: '',
email: '',
months: ''
});
const handleChangeForm = name => event => {
setValues({ ...values, [name]: event.target.value });
};
then you can use the handler and the values in infoForm as properties like this:
<Form values={values} handleChangeForm={handleChangeForm}/>
also you need to send to your button ponent the values as properties
Inside form ponent you should destructure the props like this:
const { values, handleChangeForm } = props;
return (
<form noValidate autoComplete="off"> .....
onChange event example inside form
onChange={handleChangeForm("name")}
Finally you have the values connected to your buttom ponent and you can use a function like this
const onClickBtn = () => {
console.log(props.values);
};
when button is clicked
<Button
variant="contained"
color="primary"
disabled={!props.isEnabled}
type="submit"
onClick={onClickBtn}
>
Submit
</Button>