In my articles, I'm going to be using either expo or snack online IDE and android emulator.

在我的文章中,我将使用expo或点心在线IDE和android模拟器。

React Hooks is simply an awesome tool that helps us use states and other react features without writing a class component. That is, it allows you to operate react js states inside function components.

React Hooks只是一个了不起的工具,它可以帮助我们使用状态和其他react功能而无需编写类组件。 也就是说,它允许您在功能组件内部操作react js状态。

The Hook we are using in this tutorial is called the useState Hook because there is also the effect hook.

我们在本教程中使用的Hook称为useState Hook,因为也有效果挂钩。

The useState hook takes an argument which is the initial state of the state you are building.

useState挂钩接受一个参数,该参数是您正在构建的状态的初始状态。

The developers of this say; It returns the current state value and a function that allows you to update it when it is called in an event handler.

开发者这样说; 它返回当前状态值和一个函数,该函数允许您在事件处理程序中调用它时对其进行更新。

Here are sample and output.

这是示例和输出。

Open your App's App.js file and type the following

打开您应用的App.js文件,然后输入以下内容

import * as React from 'react';
import {useState} from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
export default function App () {const [Msg,setMsg]= useState('Includehelp');
return (
<View style={styles.container}>
<Text> Hello {Msg} </Text>
<Button
title='click'
onPress={()=>setMsg('my name is Godwill')}
/>
</View>
);
}
const styles = StyleSheet.create({container: {flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});

Output

输出量

From the code above, you can see the format of creating a new react js hook. The hook is immediately before the return statement.

从上面的代码中,您可以看到创建新的react js钩子的格式。 挂钩在return语句之前。

The brackets after the const keyword contain the state called Msg and the setMsg which allows us the reset the Msg state anytime from the code.

const关键字后的方括号包含称为Msg的状态和setMsg ,这使我们可以随时从代码中重置Msg状态。

Take note of importing useState from 'react' and not react-native.

请注意从“ react”而不是“ react-native”导入useState。

The state is then reset after the button Press which changes the statement in our output.

然后,在按下按钮后,状态将被重置,按钮将更改输出中的语句。

So that's the format and brief explanation on hooks.

这就是钩子的格式和简要说明。

Thanks for coding with me! See you @ the next article. Feel free to drop a comment or question. God Bless You!

感谢您与我编码! 下次见。 随意发表评论或问题。 上帝祝福你!

翻译自: https://www.includehelp.com/react-js/how-to-use-react-js-hooks-in-react-native.aspx

如何在React Native中使用React JS Hooks?相关推荐

  1. 如何在React Native中写一个自定义模块

    前言 在 React Native 项目中可以看到 node_modules 文件夹,这是存放 node 模块的地方,Node.js 的包管理器 npm 是全球最大的开源库生态系统.提到npm,一般指 ...

  2. 如何在React Native中记录日志?

    本文翻译自:How to do logging in React Native? 如何为Web开发时在React Native中记录变量,例如使用console.log ? #1楼 参考:https: ...

  3. 如何在 React Native 中写一个自定义模块

    前言 在 React Native 项目中可以看到 node_modules 文件夹,这是存放 node 模块的地方,Node.js 的包管理器 npm 是全球最大的开源库生态系统.提到npm,一般指 ...

  4. 如何在React Native中创建精美的动画加载器

    by Vikrant Negi 通过Vikrant Negi 如何在React Native中创建精美的动画加载器 (How to create a beautifully animated load ...

  5. 如何在React Native中构建项目并管理静态资源

    by Khoa Pham 通过Khoa Pham 如何在React Native中构建项目并管理静态资源 (How to structure your project and manage stati ...

  6. 如何在React Native中使用Redux Saga监视网络更改

    by Pritish Vaidya 通过Pritish Vaidya 如何在React Native中使用Redux Saga监视网络更改 (How to monitor network change ...

  7. 如何在React Native中使用react-navigation 5处理导航

    React-navigation is the navigation library that comes to my mind when we talk about navigation in Re ...

  8. 理解 React Native 中的 AJAX 请求

    曾经,大多数 Web 应用程序通过用户操作刷新整个网页以与 Web 服务器通信. 后来,AJAX(异步 JavaScript 和 XML)概念通过提供一种在后台与 Web 服务器通信的方式使 Web ...

  9. react native中一次错误排查 Error:Error: Duplicate resources

    最近一直在使用react native中,遇到了很多的坑,同时也学习到了一些移动端的开发经验. 今天在做一个打包的测试时,遇到了一个问题,打包过程中报错"Error:Error: Dupli ...

最新文章

  1. alert在asp.net中如何使用??
  2. STM32外设之GPIO的推挽输出和开漏输出模式详解
  3. ExecuteScaler的三种返回值。
  4. AI应用开发实战 - 手写识别应用入门
  5. 图片延迟加载和滑动翻页
  6. ubuntu下使用python将ppt转成图片_Ubuntu下使用Python实现游戏制作中的切分图片功能...
  7. python都用什么写代码_python都用什么写代码
  8. mysql的一些心得
  9. iOS开发多线程篇—GCD的常见用法
  10. 将Linux目录下的文件放到子目录中
  11. 旅游景点网站景区景点购票系统毕业设计毕业论文参考(3)后台管理功能
  12. “备胎”海思的蛰伏与挑战
  13. dos攻击防范 java_php DOS攻击实现代码(附如何防范)
  14. [HDU - 3709] Balanced Number (数位dp)
  15. Alexa 世界排名推进工具--阿雷克斯(转)
  16. EOS代码架构及分析(二)
  17. Visual C++ 11 中新的并发功能
  18. 关于nacos配置中心配置不成功的问题
  19. Spring HikariDataSource连接MySQL8.0
  20. GBase 8a 数据分布式存储

热门文章

  1. java修改默认字符编码_设置默认的Java字符编码?
  2. python 二进制流_Python中对字节流/二进制流的操作:struct模块简易使用教程
  3. 手机耗电统计app_华为手机有哪些功能关掉比较好?
  4. python读取字典元素笔记_Python 学习笔记 - 字典
  5. wgs84转经纬度_wgs84经纬度转为cgcs2000平面坐标
  6. redis核心技术与实战(二)缓存应用篇
  7. Redis(一):什么是NoSQL与NoSQL分类
  8. postman里测试文件上传(MultipartFile)
  9. Shell学习之结合正则表达式与通配符的使用(五)
  10. kafka 支持发布订阅