如何在React Native中使用React JS Hooks?

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

輸出量

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!

感謝您與我編碼! 下次見。 隨意發表評論或問題。 上帝祝福你!

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541585.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541585.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541585.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

華為P40pro 手機云臺_2020年目前拍照最好的手機推薦!華為P40 Pro!DXO全球榜首

目前最熱門的拍照手機自然是華為P40 Pro&#xff0c;其相機性能直接問鼎DXOMARK手機相機評分榜首。對于拍照要極求高的用戶&#xff0c;華為P40 Pro將是一個非常不錯的選擇。那么&#xff0c;華為P40 Pro除了出色的相機之外&#xff0c;其它方面表現如何呢&#xff1f;下面&…

容器性能比無容器服務器,【譯】容器 vs 無服務器(Serverless)

一些歷史不久之前&#xff0c;開發&#xff0c;部署和運維還相當復雜。在一開始&#xff0c;運維不僅需要修補程序代碼&#xff0c;還要支持物理機器。保持服務器&#xff0c;硬件與軟件處于最新狀態也是一項艱巨的任務。在2000年代&#xff0c;一個新的模型——架構即服務(Iaa…

Centos 7安裝與配置nagios監控(一)

目 錄序言(必備知識)一、安裝規劃1.1系統環境1.2所需軟件包二、配置安裝環境2.1同步時間2.2禁用SElinux2.3 xftp上傳軟件包2.4安裝郵件服務三、監控主機安裝3.1安裝nagios的運行環境3.2增加用戶3.3安裝nagios3.4配置權限3.5安裝插件3.6安裝nrpe四、遠程主機安裝4.1配置運行環境…

java字符串刪掉子串_如何從Java中的列表中刪除子列表?

java字符串刪掉子串從列表中刪除子列表 (Removing SubList from a List) Suppose, we have a list of few elements like this, 假設我們列出了一些這樣的元素&#xff0c; list [10,20,30,40,50]From the list, we have to delete a sub list between sourcing_index (inclu…

備份linux系統報錯_Linux 系統如何快速入門?分享民工哥總結的經驗

大家好&#xff0c;我是民工哥。認識或熟悉我的人都知道&#xff0c;是做運維出身的&#xff0c;所以&#xff0c;很多時候&#xff0c;有很多朋友喜歡問我一些有關運維的問題&#xff0c;比如&#xff1a;我應該如何入門Linux系統運維&#xff1f;Linux系統運維到底需要學哪些…

pe聯想服務器裝系統教程視頻,演示聯想電腦u盤重裝系統xp教程

聯想電腦U盤重裝XP系統的方法很多朋友詢問&#xff0c;其實現在很多電腦已經不支持XP系統的安裝了&#xff0c;如果你的聯想電腦是近幾年購買的&#xff0c;還是安裝win10系統比較保險。當然聯想電腦安裝系統過程中遇到問題也可以聯系人工客服。聯想電腦如何使用U盤重裝系統XP呢…

TCP Socket 粘包

&#xfeff;&#xfeff;這兩天看csdn有一些關于socket粘包&#xff0c;socket緩沖區設置的問題。發現自己不是非常清楚&#xff0c;所以查資料了解記錄一下&#xff1a; 一兩個簡單概念長連接與短連接&#xff1a;1.長連接 Client方與Server方先建立通訊連接。連接建立后不斷…

離散數學和組合數學什么關系_關系類型| 離散數學

離散數學和組合數學什么關系關系類型 (Types of Relation) There are many types of relation which is exist between the sets, 集合之間存在許多類型的關系&#xff0c; 1. Universal Relation 1.普遍關系 A relation r from set a to B is said to be universal if: R A…

springboot公共模塊打包_解決SpringBoot多模塊發布時99%的問題?

每天都會分享Java架構文章&#xff0c;喜歡的朋友關注我。ps&#xff1a;文末有彩蛋&#xff0c;驚喜等著你如果使用的是 SpringBoot 多模塊的項目&#xff0c;在發布的時候可能遇到各種各樣的問題。本文歸納了以下 8 個原則和發布時經常出現的 4 個問題的解決方案&#xff0c;…

tomcat7的數據庫連接池tomcatjdbc的25個優勢

tomcat的JDBC連接池org.apache.tomcat.jdbc.pool更換或替代嗎Apache Commons DBCP連接池。為什么我們須要一個新的連接池?這里有幾個原因: 1.DBCP 1.x是單線程的。為了成為線程安全的 共享鎖整個池在短時間內在兩個對象 分配和對象返回。注意,這并不適用 下議院DBCP 2.x。 2.D…

weakhashmap_Java WeakHashMap entrySet()方法與示例

weakhashmapWeakHashMap類entrySet()方法 (WeakHashMap Class entrySet() method) entrySet() method is available in java.util package. entrySet()方法在java.util包中可用。 entrySet() method is used to retrieve the mappings that exist in this map to be viewed in …

定義整型數組_C++數組的定義與初始化(學習筆記:第6章 01)

數組的定義與使用[1]數組是具有一定順序關系的若干相同類型變量的集合體&#xff0c;組成數組的變量稱為該數組的元素。數組的定義方括號里面列出的常量表達式是數組每一維的下標個數。數組的下標不管從哪一維它都是從0開始數的。例如&#xff1a;int a[10]; 表示a為整型數組&a…

我們正在經歷一個應用疲憊時代?

在移動互聯網時代到來之后&#xff0c;應用程序成為了智能手機必備&#xff0c;也正因為萬千開發者的參與&#xff0c;才讓移動終端充分發揮出了強大的能量&#xff0c;當然&#xff0c;這些開發者也不斷創造著造富神話&#xff0c;一個小團隊在幾個月的努力之后可能就會成為億…

Java LinkedHashMap values()方法與示例

LinkedHashMap類的values()方法 (LinkedHashMap Class values() method) values() method is available in java.util package. values()方法在java.util包中可用。 values() method is used to get all the values exist in this LinkedHashMap to be viewed in a Collection.…

語句拼接_第2課:一個周末學會R語言數據處理:表拆分和拼接

從一線收集了兩百個文件&#xff0c;要整合到一起&#xff1f;總部一張全國兩百個城市的匯總表&#xff0c;拆成兩百個小文件&#xff1f;開什么玩笑&#xff0c;難道要復制粘貼到天荒地老。。。不用這么麻煩&#xff0c;一個循環&#xff0c;一個語句&#xff0c;實現快速表拆…

Anaconda配置多spyder多python環境

作者&#xff1a;桂。 時間&#xff1a;2017-04-17 22:02:37 鏈接&#xff1a;http://www.cnblogs.com/xingshansi/p/6725298.html 前言 最近在看《統計學習方法》&#xff0c;打算配合《機器學習實戰》一起&#xff0c;可后者的代碼是基于python2.6的&#xff1a; All the co…

pytorch自定義新層demo_從頭學pytorch(十一):自定義層

自定義layer不含模型參數的layer含模型參數的layer核心都一樣,自定義一個繼承自nn.Module的類,在類的forward函數里實現該layer的計算,不同的是,帶參數的layer需要用到nn.Parameter不含模型參數的layer直接繼承nn.Moduleimport torchfrom torch import nnclass CenteredLayer(n…

java日歷類add方法_Java日歷computeTime()方法及示例

java日歷類add方法日歷類computeTime()方法 (Calendar Class computeTime() method) computeTime() method is available in java.util package. java.util包中提供了computeTime()方法 。 computeTime() method is for conversion of current field values to the ms(millisec…

C++——智能指針和RAII

該文章代碼均在gitee中開源 C智能指針hpphttps://gitee.com/Ehundred/cpp-knowledge-points/tree/master/%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88??????? 智能指針 傳統指針的問題 在C自定義類型中&#xff0c;我們為了避免內存泄漏&#xff0c;會采用析構函數的方法釋…

移除元素所有事件監聽_DOM 事件模型或 DOM 事件機制

DOM 事件模型DOM 的事件操作&#xff08;監聽和觸發&#xff09;&#xff0c;都定義在EventTarget接口。所有節點對象都部署了這個接口&#xff0c;其他一些需要事件通信的瀏覽器內置對象&#xff08;比如&#xff0c;XMLHttpRequest、AudioNode、AudioContext&#xff09;也部…