我是Redux和Redux工具包的新手.我正在制作TODO应用程序来学习它. 在那里我可以添加和删除待办事项.现在我正在试着编辑它.然而,我不知道如何同时发送要添加到文本输入中的文本的有效负载(即"新的"待办事项)和键,以便我知道我按下了哪个键.也许我错过了一些简单的东西,或者是我需要学习的东西我误解了?希望我的代码不会乱七八糟的理解,谢谢.

THIS IS MY SLICE:

export const todoSlice = createSlice({
  name: "todo",
  initialState:{todos:[{text:"test1", key:1}, {text:"test2", key:2}, {text:"test3", key:3}]},
  reducers: {
    addTodo: (state, action) => {
      const todo = {key: Math.random(), text: action.payload,};
      state.todos.push(todo);
    },
    removeTodo: (state, action) => {
        state.todos = state.todos.filter((todo) => todo.key !== action.payload);
      },
    clearAll: (state) => {
      state.todos = []
    },
    editTodo:(state, action) => {
      //This is where I want to update the text
    }
  },
});

THIS IS THE APP:

export default function Home() {
    const [text, setText] = useState("")
    const [showModal, setShowModal] = useState(false)
    const [modalText, setModalText] = useState()
    const [newText, setNewText] = useState("")

    const [currentItem, setCurrentItem] = useState()

    const todos = useSelector((state) => state.todo.todos);
    const dispatch = useDispatch();

    const handleOnChangeText = (val) => {
        setText(val)
    }
        
    const handleAddTodo = () => {
        if (text.length > 0){
            dispatch(addTodo(text))
            setText("")
        } else {
            console.log("error")
        }
    }

    const handleRemoveTodo = (item) => {
            dispatch(removeTodo(item.key))
    }

    const handleClearAll = () => {
            dispatch(clearAll())
    }

    const handleOnEditPress = (item) => {
        setModalText(item.text)
        setShowModal(true)
        setCurrentItem(item)
    }

    const handleEditTodoText = (val) => {
        setNewText(val)
    }

    const handleOnSavePress = () => { 
        dispatch(editTodo(newText))
    }

  return (
    <SafeAreaView style={styles.container}>
{ showModal ?    <EditModule
        onClosePress={()=>setShowModal(false)}
        title={modalText}
        placeholder={modalText}
        onChangeText={(val)=>handleEditTodoText(val)}
        onSavePress={()=>handleOnSavePress()}
        /> : null}
        <View style={styles.topSection}>
            <Text>Add text</Text>
            <TextInput
                placeholder='Write here'
                style={styles.textinput}
                onChangeText={(val)=>handleOnChangeText(val)}
                value={text}
                onBlur={()=>handleAddTodo()}
            />
            <Button
                text={"Add"}
                onPress={()=>handleAddTodo()}
            />
            <Button
                text={"Clear all"}
                onPress={()=>handleClearAll()}
            />
        </View>
      
        <View style={styles.bottomSection}>
        <FlatList
            data={todos}
            renderItem={ ({item}) => (
                <ListItem 
                text={item.text}
                onPress={()=>handleRemoveTodo(item)}
                onEditPress={()=>handleOnEditPress(item)}
                />)} 
                />

        </View>
    </SafeAreaView>
  )
}

推荐答案

@罗生门给出了答案."操作的有效负载应该是同时包含键和文本的对象.例如:{key:1,Text:""已编辑的待办事项"}."

Reactjs相关问答推荐

我的组件在jsx文件中继续重新渲染

删除表2的收件箱未按预期工作

使用useReducer时,props 未从父级传递给子或孙级

如何在React中的textarea中显示字符数?

XChaCha20-Poly1305的解密函数

如何使数据库数据在第一次呈现时显示?

一键MUI导出

Reaction中的数据加载不一致

MUI 日期 Select 器 - 最小日期混乱

React Todo List 应用程序我在实现删除功能时遇到问题

BrowserRouter改变了URL但没有链接到页面

React:如何使用条件渲染和react 挂钩来更新另一个组件的状态?

单击按钮时如何添加或删除 3d 对象

如何通过单击多个选项中的特定项目在模态上呈现不同的标题?

Material UI:使用 mui 手风琴时出现props 类型失败:isValidElement 不是函数

从一个获取 axios 请求(ReactJS)中删除默认参数

在 redux 工具包中使用 dispatch 发送多个操作

axios post方法中的请求失败,状态码为500错误

RTK 查询 POST 方法不会改变数据

material uireact ,如何从表格中删除阴影?试图找到 api 但找不到正确的属性