reactjs - 在仅功能组件中设置 React Context
问题描述
我的目标很简单。我只是想从一个可重用的纯函数(无状态?)反应组件中设置我的反应上下文。
当这个可重用函数被调用时,它会将上下文(内部状态)设置为我提供的值。问题当然是您不能在仅功能组件中导入 react,因此我无法在整个应用程序中设置上下文。
没有什么可以真正表明它是一个简单的问题。
但以防万一:
<button onCLick={() => PlaySong()}></button>
export function PlaySong() {
const {currentSong, setCurrentSong} = useContext(StoreContext) //cannot call useContext in this component
}
如果我使用常规的反应组件,我不能在点击时调用这个函数:
export default function PlaySong() {
const {currentSong, setCurrentSong} = useContext(StoreContext) //fine
}
但:
<button onCLick={() => <PlaySong />}></button> //not an executable function
一个解决方案:我知道我可以通过简单地创建一个 Playbtn 组件并将其放置在每首歌曲中以便它播放歌曲来轻松解决这个问题。这种方法的问题是我正在使用一个 react-player 库,所以我不能在其中放置一个 Playbtn 组件......
解决方案
你这么近!您只需要在函数组件内定义回调。
export const PlaySongButton = ({...props}) => {
const {setCurrentSong} = useContext(StoreContext);
const playSong = () => {
setCurrentSong("some song");
}
return (
<button
{...props}
onClick={() => playSong()}
/>
)
}
如果您想要更高的可重用性,您可以创建自定义挂钩来使用您的上下文。当然你在哪里使用这些还是要遵守hooks 的规则。
export const useSetCurrentSong = (song) => {
const {setCurrentSong} = useContext(StoreContext);
setCurrentSong(song);
}
可以通过渲染一个组件来触发一个钩子函数,但是你不能像你试图做的那样调用一个组件。
const PlaySong = () => {
const {setCurrentSong} = useContext(StoreContext);
useEffect( () => {
setCurrentSong("some song");
}, []
}
return null;
}
const MyComponent = () => {
const [shouldPlay, setShouldPlay] = useState(false);
return (
<>
<button onClick={() => setShouldPlay(true)}>Play</button>
{shouldPlay && <PlaySong />}
</>
)
}
推荐阅读
- java - 如何防止 LibGDX 中的瓦片地图中的纹理渗出
- latex - Sharelatex 仅在第一次参考时生成点,而不是参考
- javascript - react js应用程序中的firebase没有在状态上设置auth对象
- python - 如何使用 Selenium 从动态网站中抓取数据
- redis - 没有 redis 的 Django 频道
- api - 如何为 Kubernetes 准入控制器编写响应
- java - 新关键字和实例关键字有什么区别?
- react-native - 使用带有 React 本机 init 的 yarn 工作区时缺少依赖项
- python - cv2.COLOR_BGR2GRAY - 改变频道号
- php - Laravel 作业推送到队列事件