博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
构建一个react项目_用React构建一个简单的计数器
阅读量:2504 次
发布时间:2019-05-11

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

构建一个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 .

让我们为此使用Codepen。 我们首先分叉 。

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 }const App = () => {  let count = 0  return (    
{count}
)}ReactDOM.render(
, 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 }const App = () => {  let count = 0  const incrementCount = increment => {    //TODO  }  return (    
{count}
)}ReactDOM.render(
, 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 } = Reactconst Button = ({ increment, onClickFunction }) => {  const handleClick = () => {    onClickFunction(increment)  }  return }const App = () => {  const [count, setCount] = useState(0)  const incrementCount = increment => {    setCount(count + increment)  }  return (    
{count}
)}ReactDOM.render(
, 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 react counter

The complete example code can be seen at

完整的示例代码可以在

翻译自:

构建一个react项目

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

你可能感兴趣的文章
探讨和比较Java和_NET的序列化_Serialization_框架
查看>>
1、jQuery概述
查看>>
数组比较大小的几种方法及math是方法
查看>>
FTP站点建立 普通电脑版&&服务器版
查看>>
js 给一段代码,给出运行后的最终结果的一些综合情况、
查看>>
webservice 详解
查看>>
js自动补全实例
查看>>
VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“
查看>>
npm 安装 sass=-=-=
查看>>
WINFORM中加入WPF控件并绑定数据源实现跨线程自动更新
查看>>
C#类对象的事件定义
查看>>
各类程序员学习路线图
查看>>
HDU 5510 Bazinga KMP
查看>>
关于select @@IDENTITY的初识
查看>>
ASP.NET MVC ajax提交 防止CSRF攻击
查看>>
关于CSS伪类选择器
查看>>
适用于带文字 和图片的垂直居中方法
查看>>
Part 2 - Fundamentals(4-10)
查看>>
使用Postmark测试后端存储性能
查看>>
NSTextView 文字链接的定制化
查看>>