博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
react钩子_如何使用useState React钩子
阅读量:2503 次
发布时间:2019-05-11

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

react钩子

Check out my first, if you’re new to them.

如果您是新手,请先查看我的 。

One React hook I most often use is useState.

我最常使用的一个React钩子是useState

import React, { useState } from 'react'

Using the useState() API, you can create a new state variable, and have a way to alter it. useState() accepts the initial value of the state item and returns an array containing the state variable, and the function you call to alter the state. Since it returns an array we use to access each individual item, like this: const [count, setCount] = useState(0)

使用useState() API,您可以创建一个新的状态变量,并可以更改它。 useState()接受状态项的初始值,并返回一个包含状态变量的数组,以及一个用来更改状态的函数。 因为它返回一个数组,所以我们使用来访问每个单独的项,如下所示: const [count, setCount] = useState(0)

Here’s a practical example:

这是一个实际的例子:

import { useState } from 'react'const Counter = () => {  const [count, setCount] = useState(0)  return (    

You clicked {count} times

)}ReactDOM.render(
, document.getElementById('app'))

You can add as many useState() calls you want, to create as many state variables as you want. Just make sure you call it in the top level of a component (not in an if or in any other block).

您可以添加useState()调用,以创建useState()状态变量。 只要确保在组件的顶层(而不是if或任何其他块中)调用它即可。

Example on Codepen:

Codepen上的示例:

See the Pen by Flavio Copes () on .

见笔由弗拉维奥COPES( 上) 。

翻译自:

react钩子

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

你可能感兴趣的文章
flex布局
查看>>
python-----python的文件操作
查看>>
java Graphics2d消除锯齿,使字体平滑显示
查看>>
控件中添加的成员变量value和control的区别
查看>>
Spring Boot Docker 实战
查看>>
Div Vertical Menu ver3
查看>>
Git简明操作
查看>>
InnoDB为什么要使用auto_Increment
查看>>
课堂练习之买书打折最便宜
查看>>
定义函数
查看>>
网络虚拟化技术(二): TUN/TAP MACVLAN MACVTAP
查看>>
MQTT协议笔记之mqtt.io项目HTTP协议支持
查看>>
(转)jQuery中append(),prepend()与after(),before()的区别
查看>>
Tecplot: Legend和图像中 Dashed/Dash dot/Long dash 等虚线显示没有区别的问题
查看>>
win8 开发之旅(2) --连连看游戏开发 项目错误的总结
查看>>
视频转换工具ffmpeg
查看>>
一、 object c -基础学习第一天 如何定义一个类
查看>>
C#调用C++编译的DLL详解
查看>>
Kali Linux的安装
查看>>
我的大学生活-5-08-赵心宁
查看>>