Home>

I would like to rewrite the following setState to useState.
I tried to rewrite it myself, but there is no particular error, but the behavior is strange.
Normally I think it is setState ({}), but I am a little worried about the fact that it is setState ({}, resolve).
If anyone knows, please comment.

I want to rewrite setState to useState

Corresponding source code
const [sortingItemKey, setSortingItemKey] = React.useState (null);
const [isSorting, setIsSorting] = React.useState (false);
//abridgement
return new Promise ((resolve) =>
      this.setState (
        ({items}) =>({items})
          sortingItemKey: items [index],
          isSorting: true,
        }),
        resolve, resolve,
      ),
    );

↓ What I rewrote

return new Promise ((resolve) =>{
      setSortingItemKey (items [index]);
      setIsSorting (true);
      resolve ();
    });
What I tried

If the callback function is a console log etc., I think it should be written as it is, but if it is resolve, what should I do?
https://www.aizulab.com/blog/react-usestate-callback/

useEffect (() =>{
      // ??
    }, [sortingItemKey, isSorting]);
    return new Promise ((resolve) =>{
      setSortingItemKey (items [index]);
      setIsSorting (true);
    });