简体   繁体   English

当我尝试将它写到控制台时,React function 显示 object 为空

[英]React function is showing that object is empty when I try to write it out to the console

I am using this constant function whenever an element in a sortable list is moved:每当移动可排序列表中的元素时,我都会使用这个常量 function :

const arrayMoveMutate = (array, from, to) => {
    array.splice(
        to < 0 ? array.length + to : to,
        0,
        array.splice(from, 1)[0]
    );
};

const arrayMove = (array, from, to) => {
    array = array.slice();
    arrayMoveMutate(array, from, to);
    return array;
};

 const onSortEnd = React.useCallback(
        ({ oldIndex, newIndex }) => {
            setPlayer(player =>
                arrayMove(player, oldIndex, newIndex)
            );
            console.log('mock api POST to save new index positions: ', player);
            console.log('oldIndex: ', oldIndex);
            console.log('newIndex: ', newIndex);
        },
        []
    );

Here is the SortableList:这是可排序列表:

<SortableList player={player} onSortEnd={onSortEnd} />

Every time a move an element, I do see the oldIndex and newIndex in my console change.每次移动元素时,我都会在控制台中看到 oldIndex 和 newIndex 发生变化。

However, 'player' is always empty.但是,“播放器”始终为空。

How would I pass the 'player' object to onSortEnd so that I can do my api POST?如何将“播放器” object 传递给 onSortEnd 以便我可以进行 api POST?

thanks!谢谢!

player in the console.log statement is a reference to the hook value created in your useState hook. console.log语句中的player是对在useState挂钩中创建的挂钩值的引用。 Hence add it to the callbacks deps as below to get the fresh value, else it will always be empty within the callback:因此,将其添加到如下回调 deps 以获得新值,否则在回调中它将始终为空:

const onSortEnd = React.useCallback(({ oldIndex, newIndex }) => {
  setPlayer(player => arrayMove(player, oldIndex, newIndex));
  console.log("mock api POST to save new index positions: ", player);
  console.log("oldIndex: ", oldIndex);
  console.log("newIndex: ", newIndex);
}, [player]);

That said, this deps is only required for logging the value inside the callback.也就是说,这个 deps 仅用于记录回调中的值。 As you are passing function to setPlayer , it will always take the latest value of the state irrespective of the deps.当您将 function 传递给setPlayer时,它将始终采用 state 的最新值,而与部门无关。 Hence the state should be setting fine even with your code.因此,即使使用您的代码,state 也应该设置得很好。 If still facing issues, try setting the deps and try again.如果仍然遇到问题,请尝试设置部门并重试。

暂无
暂无

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

相关问题 React(Jumpsuit)-除非我尝试/抓住,否则错误不会显示在控制台中 - React (Jumpsuit) - errors not showing in console unless I try/catch 当我尝试使用 svg 图标时反应返回 [object Object] - React return [object Object] when i try use svg icon 尝试在 React 中渲染 html 标记时,我得到 [Object object] - I get [Object object] when try to render html tag in React React,我想在一个function中打印出一个object - React, I want to print out an object in a function 为什么当我尝试访问它的数组时,传递给第二个 function 的 object(包含一个数组)会给出一个空数组? - Why does the object (containing an array) passed to second function, give an empty array when I try to access its array? 当 map 函数在代码中时,它返回 undefined,当 console.log 时,它返回一个空对象和一个数据。 我只想要数据 - when map function is in the code it returns undefined and when console.log it returns the an empty object and an the data. i just want the data 我正在尝试向我的反应组件发出 axiosGET 请求,我在 console.log 上获取对象。 但是当我尝试渲染它时,我得到一个“未定义” - i'm trying to make a axiosGET request to my react component, i get the object on the console.log. But when i try to render it i get a "is not defined" 为什么我必须在反应上下文对象中创建空函数? - why I have to make empty function in react context object? 为什么当我尝试使用 redux 控制台 state 并反应 js 时,我的操作结果数据是 null - Why my action result data is null when I try console the state using redux and react js 部署到 netlify 后,没有显示一页反应项目。 控制台显示错误“TypeError: Object(...) is not a function” - After deploying to netlify one page of react project is not showing. Console shows error "TypeError: Object(...) is not a function"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM