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

    相关文章

    React+TS前台项目实战(十七)-- 全局常用组件Dropdown封装

    文章目录 前言Dropdown组件1. 功能分析2. 代码+详细注释3. 使用方式4. 效果展示 总结 前言 今天这篇主要讲全局Dropdown组件封装,可根据UI设计师要求自定义修改。 Dropdown组件 1. 功能分析 (1)通过position属性,可以控制下拉选项的位置 (2)通过传入width属性, 可以自定义下拉选项的宽度 (3)通过传入classN

    00 - React 基础

    1. React 基础 安装react指令 可参考: 官网官网使用教程 如: npx create-react-app 项目名如:npx create-react-app react-redux-pro JSX JSX 是一种 JavaScript 的语法扩展,类似于 XML 或 HTML,允许我们在 JavaScript 代码中编写 HTML。 const element =

    HTTP状态码中301与302的区别

    一.官方说法  301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于:  301 redirect: 301 代表永久性转移(Permanently Moved)。  302 redirect: 302 代表暂时性转移(Temporarily Moved )。  这是很官方的说法,那么它们的区别到底是什么呢?  1.1、什么是301转向?什么是301重定向?

    Zustand 状态管理库简介

    1. Zustand 简介 Zustand(德语中意为“状态”)是一个使用简单 API 的 React 状态管理库。它的核心思想是以状态切片(slices)的方式组织应用状态,从而实现高效的状态管理。Zustand 提供了比 Redux 更加简洁和直接的用法,同时支持异步操作和中间件。 在React开发中,状态管理是一个非常重要的概念。虽然 React 提供了 useState 和 useRe

    react实现导航守卫

    React本身并没有像Vue那样的直接名为“导航守卫”的概念,但在React Router中,我们可以通过特定的方法和技术来模拟和实现类似的功能。以下是对React Router中模拟导航守卫的详解: 1. 导航守卫的概念 导航守卫:在路由切换之前执行的钩子函数,用于控制路由的跳转。在Vue Router中,这通常用于路由鉴权,即在路由跳转之前判断用户是否有权访问目标页面。React中的模拟:

    Vue3的Teleport:Teleport是Vue3的一个新功能,它允许我们将子组件渲染到父组件以外的地方,这在处理模态框、弹出窗口等情况时非常有用

    I. Teleport 的概述 Teleport 的定义:   在 Vue 3.0 中,Teleport 是一个新的内置组件,它允许我们将任何部分的渲染内容 Teleport(传送)到 Vue 应用范围之外的地方。 换句话说,你可以控制片段,让它们在 DOM 中的任何位置渲染,而不仅仅是在当前组件内部。   Teleport 的效用和应用场景:   Teleport 的主要用途是处理在 UI

    ESP32使用按键配网并通过LED指示网络状态

    前言 上面我们已经可以通过 ESPTOUCH 和 Airkiss 给模块配网,并且存储在 nvs 中,重启后仍然可以联网,只是这样仍然不能满足我们实际的应用,这次我们增加按键作为输入,LED作为输出,实现长按按键配网,并可以通过LED指示网络状态。 添加自己的组件 为了让程序结构更加清晰,所以我们在smart_config例程的基础上做了修改,在main文件夹里新建了main.c 、smar

    【从0实现React18】 (三) 初探reconciler 带你初步探寻React的核心逻辑

    Reconciler 使React核心逻辑所在的模块,中文名叫协调器,协调(reconciler)就是diff算法的意思 reconciler有什么用? 在前端框架出现之前,通常会使用 jQuery 这样的库来开发页面。jQuery 是一个过程驱动的库,开发者需要直接调用浏览器的宿主环境 API,例如 DOM 操作等。这意味着开发者需要手动管理页面状态和交互,通过执行一系列的操作来更新页面。

    React AntDesign Layout组件布局刷新页面错乱闪动

    大家最近在使用React AntDesign Layout组件布局后刷新页面时,页面布局错乱闪动 经过组件属性的研究才发现,设置 hasSider 为 true 就能解决上面的问题,耽搁了半天的时间,接着踩坑接着加油!!! import { Layout, theme } from 'antd';import { Outlet } from "react-router-dom"impo

    React+TS 从零开始教程(2):简中简 HelloWolrd

    源码链接:https://pan.quark.cn/s/c6fbc31dcb02 这一节,我们来见识React+TS的威力,开始上手开发第一个组件,什么组件呢? 当然是简中简的 HelloWolrd组件啦。 在src下创建一个components,然后新建Hello.tsx 为什么是tsx呢,这个目的就是告诉编译器,我这个文件是支持jsx语法的,如果遇到你看不懂的标签,就当作React Ele