博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在React Native中使用React JS Hooks?
阅读量:2530 次
发布时间:2019-05-11

本文共 2310 字,大约阅读时间需要 7 分钟。

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 (
Hello {
Msg}

Output

输出量

How to use React JS Hooks in React Native?

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!

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

翻译自:

转载地址:http://iytzd.baihongyu.com/

你可能感兴趣的文章
关于typedef的用法总结(转)
查看>>
hibernate could not resolve property
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>
曹德旺
查看>>
【转】判断点在多边形内(matlab)
查看>>
java基础之集合:List Set Map的概述以及使用场景
查看>>
Python 线程 进程 协程
查看>>
骨牌覆盖问题
查看>>
iOS语言中的KVO机制
查看>>
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
Python 函数参数 传引用还是传值
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>