是否可以在本机反应中使用反应钩子? [英] Is it possible to use react hooks in react native?
问题描述
在 React 文档中,它说变量和侦听器钩子必须在组件中声明,但是当我尝试这样做时 React Native 会抱怨.
In react documentation it says that the variable and the listener hook must be declared within the component but React native complains when I try to do that.
有没有办法在 React Native 中使用 Hooks?或者有谁知道他们是否会在未来实施?
Is there any way to use the Hooks in React Native? Or does anyone know if they are going to be implemented in the future ?
推荐答案
虽然 useEffect
不起作用.当前的计划是在 0.57.5 之前响应 16.6,并允许人们选择使用带有选项 0.57.5-alpha 构建的钩子.如果你现在想使用钩子:
Sort of, though useEffect
is not working. The current plan is to move to react 16.6 by 0.57.5 and allow people to opt in to using hooks with an option 0.57.5-alpha build. If you want to use hooks now:
- 在 package.json 中将您的反应依赖更改为
- 克隆react
cd react
纱线安装
yarn build -- --type=RN_OSS
- 将
build/react-native
复制到您项目的node_modules/react-native/Libraries/Renderer
目录.
16.7.0-alpha
- Change your react dependency to
16.7.0-alpha
in package.json - Clone react
cd react
yarn install
yarn build -- --type=RN_OSS
- copy
build/react-native
to your project'snode_modules/react-native/Libraries/Renderer
directory.
你也可以使用这个非官方的react-native
build 已经包含了钩子.请记住 useEffect
根本不适用于 react-native,所以你最好等待.您可以在this github issue上阅读有关此讨论的更多信息.
You can also use this unofficial react-native
build with hooks already included. Just remember that useEffect
is not working at all with react-native, so you're better off probably waiting. You can read more about this discussion on this github issue.
钩子来了!!!它们存在于 React 中,并已被确认为 React Native 0.59 中的一项功能,您甚至可以使用 当前候选版本
Hooks are coming!!! They are live in React and have been confirmed as a feature shipping in React Native 0.59, you can even use the current release candidate
这篇关于是否可以在本机反应中使用反应钩子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!