简体   繁体   English

React SetState 不向对象数组添加新对象

[英]React SetState not adding new objects to array of objects

This code is supposed to add new tasks to the state and keep the previous objects, past posts on this have lead to the same issue that it just wont be added.这段代码应该向 state 添加新任务并保留以前的对象,过去的帖子导致了同样的问题,它只是不会被添加。

function App() {

    var [tasks, setTasks] = useState([

        {
            id: uuidv4(),
            text: 'Meeting at place',
            specify: 'todo',
        },
        {
            id: uuidv4(),
            text: 'Shopping',
            specify: 'inprogress',

        },
        {
            id: uuidv4(),
            text: 'Appointment',
            specify: 'complete',

        },
    ])

    //Add Task
    const newTask = (text) => {
        const id = uuidv4();
        var specify = 'todo'
        const makeTask = { id, ...text, specify }
        console.log(makeTask)
        console.log([...tasks, makeTask])
        setTasks(tasks => [...tasks, makeTask])
        console.log(tasks)

    }

when I log what's inside the set function it shows all 4 but it doesnt get saved into tasks当我记录集合 function 中的内容时,它显示了所有 4 但它没有保存到任务中

My drag and drop function which filters by id works with the predefined object literals but not new added tasks, and instead returns undefined with new tasks.我的拖放 function 按 id 过滤适用于预定义的 object 文字,但不是新添加的任务,而是返回未定义的新任务。

Edit: useEffect was solution to correctly show my state for those with same issue.编辑:useEffect 是为那些有同样问题的人正确显示我的 state 的解决方案。 My issue was more complex due to react dnd and found solution here React Hooks: Can't access updated useState variable outside useEffect?由于反应 dnd 并在此处找到解决方案,我的问题更加复杂React Hooks: Can't access updated useState variable outside useEffect? . .

React setState hook is executed asynchronously so after updating, you cannot access the updated state in that function. React setState钩子是异步执行的,所以在更新后,您无法访问 function 中更新的 state。 if you want to access to updated tasks, you can use useEffect hook:如果你想访问更新的任务,你可以使用useEffect钩子:

const newTask = (text) => {
    const id = uuidv4();
    var specify = 'todo'
    const makeTask = { id, text, specify }
    setTasks(tasks => [...tasks, makeTask])
}


useEffect(() => {
    console.log(tasks);
}, [tasks]);

You may want to give use-immer a go to handle object/array states.您可能希望给use-immer一个 go 来处理对象/数组状态。

The useImmer hook behaves the same as useState hook and updates your object states seamlessly. useImmer钩子的行为与 useState 钩子相同,并无缝更新您的 object 状态。

This is your code below but with useImmer这是下面的代码,但使用 useImmer

import { useImmer } from "use-immer";

function App() {
  const [tasks, updateTasks] = useImmer([

        {
            id: uuidv4(),
            text: 'Meeting at place',
            specify: 'todo',
        },
        {
            id: uuidv4(),
            text: 'Shopping',
            specify: 'inprogress',

        },
        {
            id: uuidv4(),
            text: 'Appointment',
            specify: 'complete',

        },
    ])

    //Add Task
    const newTask = (text) => {
        const id = uuidv4();
        const specify = 'todo'
        const makeTask = { id, ...text, specify }
        updateTasks(draft => draft.push(metaTask));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM