Redux 工具包布尔切换没有触发
Redux toolkit boolean toggle didn't fire
我正在尝试使用布尔值来切换删除图标,但我不知道调度是如何触发的:
这里是reducer
import { createSlice } from "@reduxjs/toolkit";
const NoteToggle = {
enableDeleteButton: false,
};
const toggleReducer = createSlice({
name: "toggleReducer",
initialState: NoteToggle,
reducers: {
On: (state) => {
const newToggle = { ...state, enableDeleteButton: true };
console.log("new toogle", newToggle);
},
Off: (state) => {
const newToggle = { ...state, enableDeleteButton: false};
return newToggle;
},
},
});
export default toggleReducer.reducer;
export const { On, Off } = toggleReducer.actions;
这里是我使用选择器的地方,文件名为 note.tsx
export default function Note(props: NoteItemI) {
const { note, header, date } = props;
const toggleDeleteStatus = useSelector(
(state: RootState) => state.toggle.enableDeleteButton
);
const dateFormat = new Date(date);
const showDate = dateFormat.toLocaleString("default", {
month: "short",
day: "2-digit",
});
console.log("show date", typeof date);
这里是我使用 dispatch 的地方,文件名为 note-list
export default function NoteList(props: noteListI) {
const { title, note, id, date } = props;
const nav = useNavigation();
console.log("DaTe", new Date(date).getFullYear());
const onNavDetail = () => {
nav.navigate("Edit note", {
date: date,
note: note,
header: title,
id: id,
});
};
const dispatch: AppDispatch = useDispatch();
const toggleDeleteButton = () => {
dispatch(On);
};
return (
<View>
<TouchableOpacity
onLongPress={() => {
dispatch(On);
console.log("is clicked");
}}
flex
style={CONTAINER}
onPress={onNavDetail}
>
<Note note={note} header={title} date={date} />
</TouchableOpacity>
</View>
);
}
但是感觉dispatch没有成功,求助,万分感谢
dispatch
动作生成器 return 函数。
因此,将 dispatch(On)
更改为 dispatch(On())
我正在尝试使用布尔值来切换删除图标,但我不知道调度是如何触发的:
这里是reducer
import { createSlice } from "@reduxjs/toolkit";
const NoteToggle = {
enableDeleteButton: false,
};
const toggleReducer = createSlice({
name: "toggleReducer",
initialState: NoteToggle,
reducers: {
On: (state) => {
const newToggle = { ...state, enableDeleteButton: true };
console.log("new toogle", newToggle);
},
Off: (state) => {
const newToggle = { ...state, enableDeleteButton: false};
return newToggle;
},
},
});
export default toggleReducer.reducer;
export const { On, Off } = toggleReducer.actions;
这里是我使用选择器的地方,文件名为 note.tsx
export default function Note(props: NoteItemI) {
const { note, header, date } = props;
const toggleDeleteStatus = useSelector(
(state: RootState) => state.toggle.enableDeleteButton
);
const dateFormat = new Date(date);
const showDate = dateFormat.toLocaleString("default", {
month: "short",
day: "2-digit",
});
console.log("show date", typeof date);
这里是我使用 dispatch 的地方,文件名为 note-list
export default function NoteList(props: noteListI) {
const { title, note, id, date } = props;
const nav = useNavigation();
console.log("DaTe", new Date(date).getFullYear());
const onNavDetail = () => {
nav.navigate("Edit note", {
date: date,
note: note,
header: title,
id: id,
});
};
const dispatch: AppDispatch = useDispatch();
const toggleDeleteButton = () => {
dispatch(On);
};
return (
<View>
<TouchableOpacity
onLongPress={() => {
dispatch(On);
console.log("is clicked");
}}
flex
style={CONTAINER}
onPress={onNavDetail}
>
<Note note={note} header={title} date={date} />
</TouchableOpacity>
</View>
);
}
但是感觉dispatch没有成功,求助,万分感谢
dispatch
动作生成器 return 函数。
因此,将 dispatch(On)
更改为 dispatch(On())