是否可以在本机反应中使用反应钩子? [英] Is it possible to use react hooks in react native?

查看:62
本文介绍了是否可以在本机反应中使用反应钩子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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:

  1. package.json
  2. 中将您的反应依赖更改为 16.7.0-alpha
  3. 克隆react
  4. cd react
  5. 纱线安装
  6. yarn build -- --type=RN_OSS
  7. build/react-native 复制到您项目的 node_modules/react-native/Libraries/Renderer 目录.
  1. Change your react dependency to 16.7.0-alpha in package.json
  2. Clone react
  3. cd react
  4. yarn install
  5. yarn build -- --type=RN_OSS
  6. copy build/react-native to your project's node_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆