简体   繁体   English

useRecoilCallback 在点击时不更新 state

[英]useRecoilCallback not updating state on click

Why is this code not working?为什么这段代码不起作用?

The updateCurrentSong is not even running on click updateCurrentSong甚至没有在点击时运行


    const currentSong = useRecoilValue(currentSongState)

    const updateCurrentSong = useRecoilCallback(({snap, set}) => () => {
        set(currentSongState, prev => ({...prev, playing: !currentSong.playing}))
    }, [currentSong])

    return (
        <div onClick={updateCurrentSong()} >
   

I know this is late, but I think it's because you have parentheses in your onClick attribute.我知道这已经晚了,但我认为这是因为您的 onClick 属性中有括号。 I believe it should be like this:我相信它应该是这样的:

<div onClick={updateCurrentSong}>
...
</div>

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

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