以下是触发操作并监视更改的组件:
import { Box, Button, Stack, TextField, Typography } from "@mui/material";
import React from "react";
import { useDispatch, useSelector } from "react-redux";
import { v4 as uuidv4 } from "uuid";
import {
handleDirectorAdd,
handleDirectorInfoFormChange,
handleDirectorRemove,
} from "../../../../redux/slices/directorInfo";
const DirectorsInformation = () => {
const dispatch = useDispatch();
const directorsInfo = useSelector((state) => state.directorInfo);
const handleChange = (e, index) => {
const { name, value } = e.target;
if (name && value) {
dispatch(handleDirectorInfoFormChange({ name, value, index }));
}
};
const handleAdd = () => {
dispatch(handleDirectorAdd());
};
const handleRemove = (e, index) => {
dispatch(handleDirectorRemove({ index }));
};
return (
<form
onSubmit={handleSubmit}
noValidate
style={{
padding: "1rem",
backgroundColor: "white",
border: "1px solid darkgrey",
textAlign: "center",
}}
>
<Typography variant="body1">
<strong>Director's Information</strong>
</Typography>
<hr />
{directorsInfo?.map((directorInfo, index) => (
<div key={uuidv4()}>
<Stack
spacing={1}
alignItems="center"
style={{
padding: "1rem",
backgroundColor: "rgb(227, 227, 227)",
}}
>
<Typography variant="body1">Director's Information</Typography>
<Button
variant="contained"
color="error"
sx={{ textTransform: "none" }}
onClick={(e) => handleRemove(e, index)}
>
Remove
</Button>
<Typography variant="body1">
<strong>Full Name</strong>
</Typography>
<TextField
fullWidth
size="small"
label="Full Name"
name="name"
value={directorInfo.name}
onChange={(e) => handleChange(e, index)}
sx={{ backgroundColor: "white" }}
/>
<Typography variant="body1">
<strong>Official Email</strong>
</Typography>
<TextField
fullWidth
size="small"
label="Official Email"
name="email"
type="email"
value={directorInfo.email}
onChange={(e) => handleChange(e, index)}
sx={{ backgroundColor: "white" }}
/>
<Typography variant="body1">
<strong>Contact Number</strong>
</Typography>
<TextField
fullWidth
size="small"
label="Contact Number"
name="contactNumber"
value={directorInfo.contactNumber}
onChange={(e) => handleChange(e, index)}
sx={{ backgroundColor: "white" }}
/>
<Typography variant="body1">
<strong>Title</strong>
</Typography>
<TextField
fullWidth
size="small"
label="Tile"
name="title"
type="string"
value={directorInfo.title}
onChange={(e) => handleChange(e, index)}
sx={{ backgroundColor: "white" }}
/>
</Stack>
<Button
variant="contained"
color="success"
sx={{ textTransform: "none", margin: "2rem 0" }}
onClick={handleAdd}
>
Add Directors Information
</Button>
</div>
))}
</form>
);
};
export default DirectorsInformation;
现在减速器和动作如下所示:
import { createSlice, current } from "@reduxjs/toolkit";
const initialValues = {
name: "",
email: "",
contactNumber: "",
username: "",
title: "",
};
const initialState = [initialValues];
const directorInfo = createSlice({
name: "directorInfo",
initialState,
reducers: {
handleDirectorAdd(state, action) {
return [...state, initialValues];
},
handleDirectorRemove(state, action) {
return state.filter((_, index) => index !== action.payload.index);
},
handleDirectorInfoFormChange(state, action) {
const { index, name, value } = action.payload;
return state.map((director, idx) => {
if (idx === index) {
return {
...director,
[name]: value,
};
} else {
return director;
}
});
},
},
});
export const {
handleDirectorAdd,
handleDirectorRemove,
handleDirectorInfoFormChange,
} = directorInfo.actions;
export default directorInfo.reducer;
我面临的问题是,当我更改表单输入字段中的值时,该值会更新,但会导致重新呈现(我假设这是因为移除了对输入字段的关注).当更改任何控制器的值或添加和删除它们时,我正在try 更新REDUX存储中的状态.任何帮助都将不胜感激.
谢谢
我试图改变这个州.这个状态正在正确地变化,但是它导致了重新渲染,这是我不希望的.我已经try 了过滤功能,而不是 map ,但仍然无法达到预期的结果.我希望用户可以在输入框内流畅地书写