构建一个react项目_用React构建一个简单的计数器

2023-11-02

构建一个react项目

In this short tutorial we’ll build a very simple example of a counter in React, applying many of the concepts and theory outlined before.

在这个简短的教程中,我们将使用之前概述的许多概念和理论构建一个非常简单的React计数器示例。

Let’s use Codepen for this. We start by forking the React template pen.

让我们为此使用Codepen。 我们首先分叉React模板笔

In Codepen we don’t need to import React and ReactDOM as they are already added in the scope.

在Codepen中,我们不需要导入React和ReactDOM,因为它们已经在范围中添加了。

We show the count in a div, and we add a few buttons to increment this count:

我们以div为单位显示计数,并添加一些按钮以增加该计数:

const Button = ({ increment }) => {
  return <button>+{increment}</button>
}

const App = () => {
  let count = 0

  return (
    <div>
      <Button increment={1} />
      <Button increment={10} />
      <Button increment={100} />
      <Button increment={1000} />
      <span>{count}</span>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('app'))

Let’s add the functionality that lets us change the count by clicking the buttons, by adding a onClickFunction prop:

让我们添加一些功能,通过单击按钮,添加onClickFunction来更改计数:

const Button = ({ increment, onClickFunction }) => {
  const handleClick = () => {
    onClickFunction(increment)
  }
  return <button onClick={handleClick}>+{increment}</button>
}

const App = () => {
  let count = 0

  const incrementCount = increment => {
    //TODO
  }

  return (
    <div>
      <Button increment={1} onClickFunction={incrementCount} />
      <Button increment={10} onClickFunction={incrementCount} />
      <Button increment={100} onClickFunction={incrementCount} />
      <Button increment={1000} onClickFunction={incrementCount} />
      <span>{count}</span>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('app'))

Here, every Button element has 2 props: increment and onClickFunction. We create 4 different buttons, with 4 increment values: 1, 10 100, 1000.

在这里,每个Button元素都有两个道具: incrementonClickFunction 。 我们创建4个不同的按钮,并带有4个增量值:1、10 100、1000。

When the button in the Button component is clicked, the incrementCount function is called.

单击“按钮”组件中的按钮时,将调用incrementCount函数。

This function must increment the local count. How can we do so? We can use hooks:

此功能必须增加本地计数。 我们该怎么做? 我们可以使用钩子:

const { useState } = React

const Button = ({ increment, onClickFunction }) => {
  const handleClick = () => {
    onClickFunction(increment)
  }
  return <button onClick={handleClick}>+{increment}</button>
}

const App = () => {
  const [count, setCount] = useState(0)

  const incrementCount = increment => {
    setCount(count + increment)
  }

  return (
    <div>
      <Button increment={1} onClickFunction={incrementCount} />
      <Button increment={10} onClickFunction={incrementCount} />
      <Button increment={100} onClickFunction={incrementCount} />
      <Button increment={1000} onClickFunction={incrementCount} />
      <span>{count}</span>
    </div>
  )
}

ReactDOM.render(<App />, document.getElementById('app'))

useState() initializes the count variable at 0 and provides us the setCount() method to update its value.

useState()将count变量初始化为0,并向我们提供setCount()方法以更新其值。

We use both in the incrementCount() method implementation, which calls setCount() updating the value to the existing value of count, plus the increment passed by each Button component.

我们在incrementCount()方法实现中都使用了这两种方法,该方法调用setCount()将值更新为count的现有值,再加上每个Button组件传递的增量。

The complete example code can be seen at https://codepen.io/flaviocopes/pen/QzEQPR

完整的示例代码可以在https://codepen.io/flaviocopes/pen/QzEQPR中找到

翻译自: https://flaviocopes.com/react-example-counter/

构建一个react项目

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

构建一个react项目_用React构建一个简单的计数器 的相关文章

随机推荐

  • 网络安全是什么?如何成为一位优秀的网络安全工程师/黑客?

    网络安全是什么 首先说一下什么是网络安全 网络安全工程师工作内容具体有哪些 网络安全是确保网络系统的硬件 软件及其系统中的数据受到保护 不因偶然的或者恶意的原因而受到破坏 更改 泄露 系统连续可靠正常地运行 保障网络服务不被中断 网络安全工
  • java实现短链接得到长链接!!!

    java实现短链接得到长链接 重点 params setParameter ClientPNames HANDLE REDIRECTS false 禁止重定向 不设置 有些短链接 获取不到headers里的Location HttpClie
  • chrome 全屏模式 隐藏地址栏_6个Chrome隐藏的小技巧,你可能不知道但很实用

    Chrome占据了浏览器的大半壁江山 不少人也是将它作为电脑的默认浏览器 而它也确实非常强大 拥有着非常快的速度以及丰富的插件 同时它也隐藏了不少实用的功能 通过挖掘它们让我们更加意识到Chrome的强大 以下便是我们收集的6个不为大众所熟
  • Linux用户环境变量、系统环境变量和PATH变量

    目录 一 用户环境变量 二 系统环境变量 三 PATH变量 1 修改PATH环境变量 一 用户环境变量 PS 修改文件执行权限案例 1 在文本编辑器中新建一个shell脚本 直接执行这个文件会发现权限不够 以详细模式查看这个文件的权限 发现
  • OpenCV——Sobel边缘检测

    目录 一 Sobel算法 1 算法概述 2 主要函数 二 C 代码 三 python代码 四 结果展示 1 灰度图 2 X方向一阶边缘 2 Y方向一阶边缘 3 整幅图像的一阶边缘 五 相关链接 一 Sobel算法 1 算法概述 Sobel边
  • matplotlib库使用教程:这一篇就够了

    一 导入库 import matplotlib pyplot as plt 二 显示图片 plt imshow imge 负责对图像进行处理 imge类型
  • Zotero安装教程(非常详细)从零基础入门到精通,看完这一篇就够了

    Zotero安装及简单配置 1 引言 Zotero是目前最符合我对文献管理软件需求的一款 在这里简单介绍下其安装教程及我在使用的插件 2 安装及同步设置 2 1 下载 前往官网https www zotero org 点击Download按
  • J2EE&反射

    文章目录 什么是反射 类类 反射实例化 反射动态方法调用 反射读写属性 源代码 什么是反射 Java语言的一种机制 通过这种机制可以动态的实例化对象 读写属性 调用方法 类类 类类 描述类的类 不是官方定义的语言 Class forName
  • Flutter开发篇 TextField和TextFromField

    TextFiled和TextFromField都是用来输入的 但是也是有区别的 尤其是方法有很大的区别 大家可以分别查看源码文档 在资料比较少的情况下那是最快的学习方法 TextEditingController controller Te
  • git创建本地分支,远程分支

    一 本地分支 创建本地分支 然后切换到dev分支 git checkout b dev git checkout命令加上 b参数表示创建并切换 相当于以下两条命令 git branch dev git checkout dev 然后 用gi
  • word自动编号设置方法

    需求是使用word时自动生成如下类型的自动编号 第一章 项目详细设计 1 1 视频监控系统 1 1 1 前端子系统 1 1 1 1 球机 第二章 案例介绍 2 1 某某区平安城市介绍 实现方法 1 首先点击 多级列表 定义新的多级列表 2
  • python爬虫-数据可视化-气温排行榜

    本文的文字及图片来源于网络 仅供学习 交流使用 不具有任何商业用途 版权归原作者所有 如有问题请及时联系我们以作处理 以下文章来源于腾讯云 作者 py3study 想要学习Python Python学习交流群 1039649593 满足你的
  • vulnhub靶机Brainpan

    主机发现 arp scan l 端口扫描 nmap min rate 10000 p 192 168 21 156 服务扫描 nmap sV sT O p9999 10000 192 168 21 156 这个地方感到了有点不对劲 pyth
  • 粒子滤波(Particle filter)算法简介及MATLAB实现

    粒子滤波是以贝叶斯推理 点击打开链接 和重要性采样为基本框架的 因此 想要掌握粒子滤波 对于上述两个基本内容必须有一个初步的了解 重要性采样呢 其实就是根据对粒子的信任程度添加不同的权重 添加权重的规则就是 对于我们信任度高的粒子 给它们添
  • 解决pip安装numpy问题:ERROR: Failed building wheel for numpy/ERROR: numpy-1.22.4+mkl-cp38-cp38-win_amd64.wh

    出现过问题 ERROR Failed building wheel for numpy 下载了whl文件后报错ERROR numpy 1 22 4 mkl cp38 cp38 win amd64 whl is not a supported
  • java怎么关闭fxml窗口,如何关闭窗口关闭JavaFX应用程序?

    In Swing you can simply use setDefaultCloseOperation to shut down the entire application when the window is closed Howev
  • 因果推理(八):工具变量(Intrusmental Variables)

    关于因果关系的识别 前面介绍了一些方法 随机对照试验 后门调整 前门调整 do 演算 今天介绍另一种进行因果效应识别的另一种方法 工具变量 1 什么是工具变量 上面的因果图中 Z Z Z就是一个工具变量 可以利用它在 U U U观测不到的情
  • Java贪心算法: 田忌赛马

    import java util Scanner import java util List import java util ArrayList import java util Collections public class Main
  • TensorFlow2.0简介和线性回归

    简介 废弃清除了1 0版本的多数API 使用了高级核心API tf Keras Eager模式 代码直接运行 直观调试 tf GradientTape 求解梯度 自定义训练逻辑 tf data 加载图片数据和结构化数据 tf fuction
  • 构建一个react项目_用React构建一个简单的计数器

    构建一个react项目 In this short tutorial we ll build a very simple example of a counter in React applying many of the concepts