react 渲染道具_React中的道具与状态

2023-10-21 23:59
文章标签 react 状态 渲染 道具

本文主要是介绍react 渲染道具_React中的道具与状态,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

react 渲染道具

I had been working with React and Redux on a project for a while before being asked what the difference between props and state were in React. I realized at that point that while I felt comfortable working with both, I couldn’t clearly articulate the differences between them. So, I’ve set out to both clarify the differences for myself and perhaps help others do the same.

我一直在一个项目上与React和Redux一起工作了一段时间,然后被问及React中prop和state之间的区别是什么。 那时我意识到,尽管我对两者的合作感到很自在,但我无法清楚地阐明它们之间的区别。 因此,我既要为自己澄清差异,也可能要帮助其他人做同样的事情。

The general overview is that props are variables passed from a parent component to a child component with a read-only intent. State, on the other hand, is comprised of variables that are initialized and updated directly by the component. State rather than props is used whenever a component will be updating the data.

总体概述是,道具是具有只读意图的从父组件传递到子组件的变量。 另一方面,状态由直接由组件初始化和更新的变量组成。 每当组件将要更新数据时,都会使用状态而不是道具。

MORE ON PROPS

更多关于道具

Props can be passed from a parent component inside of the rendering of the child component, as seen below. This example is passing a method and a string property down to the child component.

可以从子组件的渲染内部的父组件传递道具,如下所示。 此示例将方法和字符串属性传递给子组件。

A child component being rendered with props for a method, and a string variable for favorite color being passed to it

Passing a method down from the parent component allows you to centralize the managing of state, so your app can be composed mostly of stateless, easier to manage, functional components.

从父组件向下传递方法可以使您集中管理状态,因此您的应用程序主要由无状态,易于管理的功能组件组成。

The above example is also passing a string down to the child component to keep it simple, but this could as easily, and likely more commonly, be a piece of dynamic data handed down to the child.

上面的示例还将一个字符串向下传递给子组件,以使其保持简单,但这很容易,而且很可能是传递给子组件的动态数据。

From the perspective of the child element, all props passed from the parent component will be collected into a single props object. The way they are accessed within a child element depends on whether you have a functional or class component.

从子元素的角度来看,将从父组件传递来的所有道具都将收集到一个道具对象中。 在子元素中访问它们的方式取决于您是否具有功能或类组件。

Below is a simple functional component using props handed down by the parent component. You can think of the props like any argument handed to a function.

下面是一个简单的功能组件,使用了父组件传递的道具。 您可以将props视为传递给函数的任何参数。

A child functional component using props to display favorite color, and use a parent method

Props are read-only. They are not intended to be changed directly from the child component. However, they can be used to set an initial state that IS expected to change dynamically.

道具是只读的。 不能直接从子组件中更改它们。 但是,它们可用于设置IS有望动态更改的初始状态。

For that, we will use a class component for the child.

为此,我们将为孩子使用一个类组件。

MORE ON STATE

更多状态

Prior to the addition of hooks to React, which this post is not using, class components needed to be used to control state, because they have additional features of local state and lifecycle methods that functional components don’t have access to. Below is an example of a class child component that uses a constructor and super to inherit these react component additional features.

在向React添加钩子之前(本文未使用),需要使用类组件来控制状态,因为它们具有局部状态和功能组件无法访问的生命周期方法的附加功能。 以下是使用构造函数和super继承这些react组件其他功能的类子组件的示例。

In a class method, the props given to it by the parent component are accessed by using this.props instead of props as with the above functional component. In this case, we are using the props to set an initial state in the constructor, which will only run on the first render of this component.

在类方法中,使用this.props而不是像上面的功能组件那样使用props来访问父组件给它的props 。 在这种情况下,我们使用道具在构造函数中设置初始状态,该状态仅在该组件的第一个渲染器上运行。

Any changes to the state will need to use this.setState as seen in the example, which will trigger a re-render of the component to show the update.

如示例所示,对状态的任何更改都将需要使用this.setState ,这将触发组件的重新呈现以显示更新。

Image for post

It is important to remember that setState is an asynchronous action, so if you are adding a number, or doing something relative to the current state, you will need to pass the prevState first argument to setState, to be sure you know what the current value of your state is that you are changing.

重要的是要记住,setState是一个异步操作,因此,如果要添加数字或相对于当前状态执行某些操作,则需要将prevState第一个参数传递给setState,以确保您知道当前值是什么。你的状态是你正在改变。

For more detail, check out Components and Props and State and Lifecycle from the React documentation, or the How to pass props to components in React blog post by Robin Wieruch.

有关更多详细信息,请查看React文档中的组件和道具以及状态和生命周期 ,或者Robin Wieruch撰写的如何在React博客中将道具传递给组件 。

Learn about Using the useState Hook for State Management in React.

了解有关在React中使用useState Hook进行状态管理的信息 。

I am forever improving. If you have corrections or feedback, I would be happy to learn and make fixes. Thank you!

我永远都在进步。 如果您有更正或反馈意见,我将很乐意学习并进行修复。 谢谢!

翻译自: https://medium.com/swlh/props-vs-state-in-react-2234468a5f52

react 渲染道具


http://www.taodudu.cc/news/show-8026444.html

相关文章:

  • 完全背包 N
  • 道具的生命回复效果
  • 深度优先搜索:忍者道具
  • bolts android,Bots'n Bolts
  • Windows新建设置用户命令大全
  • spark基本命令
  • 收藏的博客 -- OpenCV/kornia/Pillow/Halcon/NI Vision/MIL/*计算机视觉
  • 写论文的开源免费神器汇总
  • 2023年noc指导教师认证测评参考试题
  • 中国政法大学计算机网课答案,大学计算机基础-网课答案
  • 并发线程 (2) - C++线程间共享数据【详解:如何使用锁操作】
  • 【C#】共享内存通信的学习(传递结构体)
  • java 多进程间的共享数据库_用内存映射在多个应用程序之间共享数据
  • IntelliJ IDEA 如何共享设置?
  • 用python画小猫简单代码,用python画最简单的图案
  • 【linux命令讲解大全】078.“joe: 强大的纯文本编辑器“
  • 关于CycleGAN源码运行时一直中断的问题解决办法
  • CVPR2022 Oral:GAN监督的密集视觉对齐
  • GAN对抗网络入门教程
  • [GAN]原始GAN论文的通俗解释
  • 【Keras-MLP-GAN】MNIST
  • gan与dcgan训练自己的数据集
  • 对抗生成网络学习(十二)——MARTA-GAN实现遥感图像的场景生成(tensorflow实现)
  • GAN笔记
  • 一个简单的拼音点读小程序的源代码
  • 代码旅程
  • 一段激动人心的旅程–读《硅谷百年史》有感
  • 一个地址的旅程
  • 一个数据包发送的旅程
  • 大学多的只是一段特殊旅程
  • 这篇关于react 渲染道具_React中的道具与状态的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/257779

    相关文章

    Flutter监听当前页面可见与隐藏状态的代码详解

    《Flutter监听当前页面可见与隐藏状态的代码详解》文章介绍了如何在Flutter中使用路由观察者来监听应用进入前台或后台状态以及页面的显示和隐藏,并通过代码示例讲解的非常详细,需要的朋友可以参考下... flutter 可以监听 app 进入前台还是后台状态,也可以监听当http://www.cppcn

    MySQL 中的服务器配置和状态详解(MySQL Server Configuration and Status)

    《MySQL中的服务器配置和状态详解(MySQLServerConfigurationandStatus)》MySQL服务器配置和状态设置包括服务器选项、系统变量和状态变量三个方面,可以通过... 目录mysql 之服务器配置和状态1 MySQL 架构和性能优化1.1 服务器配置和状态1.1.1 服务器选项

    linux进程D状态的解决思路分享

    《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

    Java实现状态模式的示例代码

    《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

    通过prometheus监控Tomcat运行状态的操作流程

    《通过prometheus监控Tomcat运行状态的操作流程》文章介绍了如何安装和配置Tomcat,并使用Prometheus和TomcatExporter来监控Tomcat的运行状态,文章详细讲解了... 目录Tomcat安装配置以及prometheus监控Tomcat一. 安装并配置tomcat1、安装

    Linux之进程状态&&进程优先级详解

    《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

    详解如何在React中执行条件渲染

    《详解如何在React中执行条件渲染》在现代Web开发中,React作为一种流行的JavaScript库,为开发者提供了一种高效构建用户界面的方式,条件渲染是React中的一个关键概念,本文将深入探讨... 目录引言什么是条件渲染?基础示例使用逻辑与运算符(&&)使用条件语句列表中的条件渲染总结引言在现代

    React实现原生APP切换效果

    《React实现原生APP切换效果》最近需要使用Hybrid的方式开发一个APP,交互和原生APP相似并且需要IM通信,本文给大家介绍了使用React实现原生APP切换效果,文中通过代码示例讲解的非常... 目录背景需求概览技术栈实现步骤根据 react-router-dom 文档配置好路由添加过渡动画使用

    hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

    利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

    hdu1565(状态压缩)

    本人第一道ac的状态压缩dp,这题的数据非常水,很容易过 题意:在n*n的矩阵中选数字使得不存在任意两个数字相邻,求最大值 解题思路: 一、因为在1<<20中有很多状态是无效的,所以第一步是选择有效状态,存到cnt[]数组中 二、dp[i][j]表示到第i行的状态cnt[j]所能得到的最大值,状态转移方程dp[i][j] = max(dp[i][j],dp[i-1][k]) ,其中k满足c