我第一次尝试使用 Redux 在我的应用程序中放入一个表单组件,并且我试图弄清楚我必须为什么创建 Reducers / Actions。
在其他组件中,我将我的用户和消息传递到 mapStateToProps 并且它们工作正常。但是,在这个组件中,我从后端为 componentDidMount 方法中的表字段提取数据,我不确定是否只有要更改的数据存储在 Redux 中。
我还需要为表单创建一个减速器吗?还是直接发布到后端/节点/postgresql。我打算有一个用所有最新数据更新的表,以便我可以看到它被自动添加到检索数据的逻辑。
我对 React / JavaScript 很陌生,所以我的逻辑可能有点偏离,所以任何建议都将不胜感激。
潜水LogForm.component.js
export class DiveLogForm extends Component {
constructor(props){
super(props);
this.handleSubmitDive = this.handleSubmitDive.bind(this);
this.onChangeDiveType = this.onChangeDiveType.bind(this);
this.onChangeSchoolName = this.onChangeSchoolName.bind(this);
this.onChangeCurrent = this.onChangeCurrent.bind(this);
this.onChangeVisibility = this.onChangeVisibility.bind(this);
this.onChangeDiveDate = this.onChangeDiveDate.bind(this);
this.onChangeMaxDepth = this.onChangeMaxDepth.bind(this);
this.onChangeDiverUserNumber = this.onChangeDiverUserNumber.bind(this);
this.onChangeVerifiedBySchool = this.onChangeVerifiedBySchool.bind(this);
this.onChangeDiveNotes = this.onChangeDiveNotes.bind(this);
this.onChangeDivePoint = this.onChangeDivePoint.bind(this);
this.state = {
diveTypeID: "",
diveSchoolNameID: "",
diveCurrentID: "",
diveVisibilityID: "",
diveDate: "",
diveMaxDepth: "",
diverUserNumber: "",
diveVerifiedBySchool: "",
diveNotes: "",
divePoint: "",
currentList: [],
regionList: [],
diveTypeList: [],
visibilityList: [],
diveSpotList: [],
currentUser: [],
loading: false,
};
}
componentDidMount() {
pullCurrentFields().then((response) => {
const { data } = response;
this.setState({ currentList: data.data });
});
pullRegionFields().then((response) => {
const { data } = response;
this.setState({ regionList: data.data });
});
pullDiveTypeFields().then((response) => {
const { data } = response;
this.setState({ diveTypeList: data.data });
});
pullVisibilityFields().then((response) => {
const { data } = response;
this.setState({ visibilityList: data.data });
});
pullDiveSpotFields().then((response) => {
const { data } = response;
this.setState({ diveSpotList: data.data });
});
//this.props.userDiveLogList();
}
onChangeDiveType(e) {
this.setState({
diveTypeID: e.target.value,
});
}
onChangeSchoolName(e) {
this.setState({
diveSchoolNameID: e.target.value,
});
}
onChangeCurrent(e) {
this.setState({
diveCurrentID: e.target.value,
});
}
onChangeVisibility(e){
this.setState({
diveVisibilityID: e.target.value,
});
}
onChangeDiveDate(e) {
this.setState({
diveDate: e.target.value,
});
}
onChangeMaxDepth(e){
this.setState({
diveMaxDepth: e.target.value,
});
}
onChangeDiverUserNumber(e){
this.setState({
diverUserNumber: e.target.value,
});
}
onChangeVerifiedBySchool(e){
this.setState({
diveVerifiedBySchool: e.target.value,
});
}
onChangeDiveNotes(e) {
this.setState({
diveNotes: e.target.value,
});
}
onChangeDivePoint(e){
this.setState({
divePoint: e.target.value,
});
}
handleSubmitDive(e) {
e.preventDefault();
this.setState({
loading: true,
});
this.form.validateAll();
//const {dispatch, history} = this.props;
if (this.checkBtn.context._errors.length === 0) {
this.props
.dispatch(registerUserDive(
this.state.diveTypeID,
this.state.diveSchoolNameID,
this.state.diveCurrentID,
this.state.diveVisibilityID,
this.state.diveDate,
this.state.diveMaxDepth,
this.state.diverUserNumber,
this.state.diveVerifiedBySchool,
this.state.diveNotes,
this.state.diveNotes))
.then(() => {
window.history.push("/divelogtable");
window.location.reload();
})
.catch(() => {
this.setState({
loading: false
});
});
}
}
render() {
const { classes } = this.props;
const { user: currentUser } = this.props;
if (this.state.currentList.length > 0) {
console.log("currentList", this.state.currentList);
}
if (this.state.regionList.length > 0) {
console.log("regionList", this.state.regionList);
}
if (this.state.diveTypeList.length > 0) {
console.log("diveTypeList", this.state.diveTypeList);
}
if (this.state.visibilityList.length > 0) {
console.log("visibilityList", this.state.visibilityList);
}
if (this.state.diveSpotList.length > 0) {
console.log("diveSpotList", this.state.diveSpotList);
}
return (
...materialUI form code
function mapStateToProps(state){
const { user } = state.auth;
const { regionList } = state.region;
const { currentList } = state.current;
const { diveTypeList } = state.diveType;
const { visibilityList } = state.visibility;
const { diveSpotList } = state.diveSpot;
return {
user,
regionList,
currentList,
diveTypeList,
visibilityList,
diveSpotList,
};
}
export default compose(
connect(
mapStateToProps,
),
withStyles(useStyles)
)(DiveLogForm);
因为我主要关心将表单数据添加到后端。我已经包含了diveLog.service.js 文件等
export const registerDive = (diveTypeID, diveSchoolNameID, diveCurrentID, diveVisibilityID, diveDate, diveMaxDepth, diveEquipmentWorn, diverUserNumber, diveVerifiedBySchool, diveNotes, divePoint) => {
return axios.post(API_URL + "registerdive", {
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diveVerifiedBySchool,
diveNotes,
divePoint
});
};
潜水日志.action.js
export const registerUserDive = (
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diverUserNumber,
diveVerifiedBySchool,
diveNotes,
divePoint) => (dispatch) => {
return registerDive(
diveTypeID,
diveSchoolNameID,
diveCurrentID,
diveVisibilityID,
diveDate,
diveMaxDepth,
diveVerifiedBySchool,
diveNotes,
divePoint).then(
(response) => {
dispatch ({
type: successful_reg,
});
dispatch({
type: set_message,
payload: response.data.message,
});
return Promise.resolve();
},
(error) => {
const message =
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
dispatch({
type: set_message,
payload: message,
});
return Promise.resolve();
},
(error) => {
(error.response &&
error.response.data &&
error.response.data.message) ||
error.message ||
error.toString();
dispatch({
type: failed_reg,
});
return Promise.reject();
}
);
};
我的diveLog 注册操作可能有点偏离,因为我在编码时不理解reducer 的概念。