如何解决当我调度时,有效负载值回到React中的旧值
// EditableNote.js
function EditableNote({ note }) {
const [editableNote,setEditableNote] = useState(note);
const { title,content } = editableNote;
const dispatch = useDispatch();
useEffect(() => {
setEditableNote(note);
},[note]);
›
useEffect(() => {
dispatch(saveEditableNote(editableNote)); // I think here is problem
},[dispatch,editableNote]);
const handleBlur = e => {
const name = e.target.id;
const value = e.currentTarget.textContent;
setEditableNote({ ...editableNote,[name]: value });
};
return (
<EditNote spellCheck="true">
<NoteTitle
id="title"
placeholder="Title"
onBlur={handleBlur}
contentEditable
suppressContentEditableWarning={true}>
{title}
</NoteTitle>
<NoteContent
id="content"
placeholder="Note"
onBlur={handleBlur}
contentEditable
suppressContentEditableWarning={true}>
{content}
</NoteContent>
</EditNote>
);
}
export default EditableNote;
我有一个editableNote组件,它是contentEditable。我通过其父项(注意)的道具设置其初始状态。因此,如果便笺中有更改,则必须更改editableNote。 为了保持最近的道具状态,我使用useEffect。一切看起来都很好。
这是一个问题。如果我先更改便笺的颜色并键入,它将按预期进行更新。但是相反,如果我第一次输入并更改颜色,则editableNote状态不会更新。
// Reducer.js
case actions.GET_NOTE_COLOR:
return {
...state,bgColor: action.payload
}
case actions.CHANGE_NOTE_COLOR:
return {
...state,notes: state.notes.map(note => note.id === action.payload ?
{ ...note,bgColor: state.bgColor }
: note
)
};
case actions.SAVE_EDITABLE_NOTE: // payload is old value
return {
...state,editableNote: action.payload,}
我检查动作中发生了什么。我发现一切正常,直到CHANGE_NOTE_COLOR,但是在调度SAVE_EDITABLE_NOTE时,其有效负载不会更新!
我不知道..请帮帮我... TT
解决方法
您必须使用connect
提供的redux
包装器将Redux的actions
和state
连接到组件
https://react-redux.js.org/api/connect
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。