我的代码中有一个有趣的问题,我不确定它为什么会发生。希望有人能解释一下。基本上,在调用“调度”方法之前,我使用“setValues”作为我的钩子。但是,似乎在更新钩子中的值之前仍然触发了调度。尽管在我看来它应该是相反的。为什么会发生这种情况?这是一些代码:
import React, { useState } from 'react';
import Grid from '@material-ui/core/Grid';
import InputField from 'shared/components/form/Inputfield';
import DatePicker from 'shared/components/pickers/DatePicker';
import InfoButton from 'shared/components/buttons/InfoButton';
import CustomButton from 'shared/components/buttons/CustomButton';
import TextArea from 'shared/components/form/TextArea';
import { connect } from 'react-redux';
export function ProjectDetails(props) {
const [values, setValues] = React.useState({
full_project_name: ' ',
short_name: ' ',
associated_projects: ' ',
short_description: ' ',
});
const handleInputFieldChange = field_name => event => {
setValues({ ...values, [field_name]: event.target.value });
//why does dispatch happen before values are updated?
props.dispatch({ type: 'PLEASE_WORK', payload: values });
};
console.log('Project values', values);
return (
<>
<h1>Project Details</h1>
<Grid container spacing={1}>
<Grid item xs={12}>
<h3>Full project name *</h3>
</Grid>
<Grid item xs={12}>
<InputField
handler={handleInputFieldChange('full_project_name')}
placeHolderText="Please type the last letter two times"
/>
</Grid>
<Grid item xs={12}>
<h3>Short name (Acronym)</h3>
</Grid>
<Grid item xs={12}>
<InputField handler={handleInputFieldChange('short_name')} />
</Grid>
</Grid>
</>
);
}
function mapStateToProps(state) {
return {
full_project_name: state.projectReducer.full_project_name,
short_name: state.projectReducer.short_name,
};
}
export default connect(mapStateToProps)(ProjectDetails);