This is my first time using Redux and I haven’t properly learnt the map feature in JS so if I wanted to use the fields that are stored in my store to populate a dropdown menu, how would I go about it?
I am aware that the “value={10}” specifies the value that gets submitted by the form to populate the table. As the foreign key values relate to a number in the table I want to submit them too, I would need to have these values as well as the strings for each “menuItem”.
diveLogForm.component.js
const DiveLogForm = (props) => {
// select user object from redux
const user = useSelector(state => state.user);
// get the object with all the fields
const fields = useSelector(state => state.fields);
// can destructure individual fields
const { schoolName, current, region, diveType, visibility, diveSpot } = fields;
.........
// all onChange functions do the exact same thing, so you only need one
// pass to a component like onChange={handleChange('typeID')}
const handleChange = (property) => (e) => {
setDive({
// override the changed property and keep the rest
...dive,
[property]: e.target.value,
});
}
// get access to dispatch
const dispatch = useDispatch();
// useEffect with an empty dependency array is the same as componentDidMount
useEffect(() => {
// dispatch the action to load fields for each field type
// once loaded, the changes will be reflected in the fields variable from the useSelector
Object.keys(fields).forEach(name => dispatch(requireFieldData(name)));
}, []); // <-- empty array
..........
return (
<form onSubmit={handleSubmitDive}>
{/*<MuiThemeProvider theme={}>*/}
<>
<AppBar title="Enter your dive details"></AppBar>
<Grid container spacing={6}
direction="row"
justify="center"
alignItems="center">
{/*<Grid item xs={1}>*/}
{/* /!*<Paper className={classes.paper}>xs=3</Paper>*!/*/}
{/*</Grid>*/}
{/*<Grid item xs={10}/>*/}
<Grid item xs={4}>
<FormControl className={classes.formControl}>
<InputLabel id="Enter-the-dive-type">Dive Type</InputLabel>
<Select
labelId="Enter-the-dive-type"
id="Enter-the-dive-typer"
value={dive.typeID}
onChange={handleChange}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</Grid>
<Grid item xs={4}>
<FormControl className={classes.formControl}>
<InputLabel id="Enter-the-region">Dive School</InputLabel>
<Select
labelId="Enter-the-dive-school"
id="Enter-the-dive-school"
value={dive.schoolNameID}
onChange={handleChange}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</Grid>
<Grid item xs={4}>
<FormControl className={classes.formControl}>
<InputLabel id="Enter-the-current-level">Current Level</InputLabel>
<Select
labelId="Enter-the-current-level"
id="Enter-the-current-level"
value={dive.currentID}
onChange={handleChange}>
<MenuItem value="">
<em>None</em>
</MenuItem>
<MenuItem value={10}>Ten</MenuItem>
<MenuItem value={20}>Twenty</MenuItem>
<MenuItem value={30}>Thirty</MenuItem>
</Select>
</FormControl>
</Grid>
...
<Grid item xs={12} md={12}>
<Button variant="primary" type="submit">
Submit</Button>
</Grid>
<br />
{/*<Grid item xs={1}>*/}
{/* /!*<Paper className={classes.paper}>xs=3</Paper>*!/*/}
{/*</Grid>*/}
</Grid>
</>
{/*</MuiThemeProvider>*/}
</form>
)
}