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

    相关文章

    IIS 7.0 及更高版本中的 FTP 状态代码

    《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

    Python Selenium动态渲染页面和抓取的使用指南

    《PythonSelenium动态渲染页面和抓取的使用指南》在Web数据采集领域,动态渲染页面已成为现代网站的主流形式,本文将从技术原理,环境配置,核心功能系统讲解Selenium在Python动态... 目录一、Selenium技术架构解析二、环境搭建与基础配置1. 组件安装2. 驱动配置3. 基础操作模

    Nginx部署React项目时重定向循环问题的解决方案

    《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

    在React聊天应用中实现图片上传功能

    《在React聊天应用中实现图片上传功能》在现代聊天应用中,除了文字和表情,图片分享也是一个重要的功能,本文将详细介绍如何在基于React的聊天应用中实现图片上传和预览功能,感兴趣的小伙伴跟着小编一起... 目录技术栈实现步骤1. 消息组件改造2. 图片预览组件3. 聊天输入组件改造功能特点使用说明注意事项

    使用Python实现IP地址和端口状态检测与监控

    《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

    SpringSecurity JWT基于令牌的无状态认证实现

    《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

    关于WebSocket协议状态码解析

    《关于WebSocket协议状态码解析》:本文主要介绍关于WebSocket协议状态码的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录WebSocket协议状态码解析1. 引言2. WebSocket协议状态码概述3. WebSocket协议状态码详解3

    在React中引入Tailwind CSS的完整指南

    《在React中引入TailwindCSS的完整指南》在现代前端开发中,使用UI库可以显著提高开发效率,TailwindCSS是一个功能类优先的CSS框架,本文将详细介绍如何在Reac... 目录前言一、Tailwind css 简介二、创建 React 项目使用 Create React App 创建项目

    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 服务器选项