最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you properly call useState within a React custom Hook? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a custom Hook that allows me to pass a value and subtract 1 whenever I press a button. I'm currently getting the error that says

React Hook "useLastPage" is called in function "handleLastPage" which is neither a React function ponent or a custom React Hook function

in the following code:

function usePrevPage (page) {
    const [lastPage, useLastPage] = useState(page)
    useEffect(() => {
        function handleLastPage(page) {
            useLastPage(page - 1)
        }
        handleLastPage()
    })
    return lastPage
}

My code mirrors the React Doc's custom hook example closely so I'm not sure how to call useLastPage within my custom Hook. Following is the example from React's Doc:

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

  return isOnline;
}

I'm trying to create a custom Hook that allows me to pass a value and subtract 1 whenever I press a button. I'm currently getting the error that says

React Hook "useLastPage" is called in function "handleLastPage" which is neither a React function ponent or a custom React Hook function

in the following code:

function usePrevPage (page) {
    const [lastPage, useLastPage] = useState(page)
    useEffect(() => {
        function handleLastPage(page) {
            useLastPage(page - 1)
        }
        handleLastPage()
    })
    return lastPage
}

My code mirrors the React Doc's custom hook example closely so I'm not sure how to call useLastPage within my custom Hook. Following is the example from React's Doc:

function useFriendStatus(friendID) {
  const [isOnline, setIsOnline] = useState(null);

  useEffect(() => {
    function handleStatusChange(status) {
      setIsOnline(status.isOnline);
    }

    ChatAPI.subscribeToFriendStatus(friendID, handleStatusChange);
    return () => {
      ChatAPI.unsubscribeFromFriendStatus(friendID, handleStatusChange);
    };
  });

  return isOnline;
}
Share Improve this question edited May 17, 2019 at 14:11 Vencovsky 31.7k22 gold badges130 silver badges195 bronze badges asked May 17, 2019 at 14:03 KevvvKevvv 4,02312 gold badges52 silver badges105 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 5

React is confused as you are using useLastPage for the updator of const [lastPage, useLastPage] = useState(page).

Try to change useLastPage to setLastPage. You should use use prefix for hooks.

Check out the demo - https://codesandbox.io/s/crazy-neumann-hgely

useEffect is similar to ponentDidMount and ponentDidUpdate. It will run when the Component is being mounted/updated. To decrease the lastPage state value you can simply call its setter directly inside the useState callback. Also I suggest changing the setter prefix to set instead of use

function usePrevPage (page) {
    const [lastPage, setLastPage] = useState(page);
    useEffect(() => {
          setLastPage(page-1);
    });
    return lastPage;
}

If you want to change the value on button click then you need to write the handler outside useEffect and set that to the button's onClick prop.

I think that your main problem is that you are calling your set function useLastPage - The React linter looks for functions that begin with the word use and tries to 'lint' them as hooks. In this case useLastPage isn't a hook, it's the returned setter from useState. I suspect that if you call it setLastPage that will clear up the error message for you.

发布评论

评论列表(0)

  1. 暂无评论