react 渲染道具_关于React道具的另一篇文章
react 渲染道具
You could say this topic has been done to death, but lately I’ve started using a technique that I don't recall having come across elsewhere. While it's not particularly clever, it is concise. So please forgive one more post on the topic...
你可以说这个话题已经做了 到 死亡 ,但最近我使用的技术,我不记得有碰到过其他地方开始。 尽管它不是特别聪明,但它很简洁。 因此,请原谅该主题的另一篇文章...
冗长的方式 (Props the verbose way)
“Don't be afraid! We won't make an author of you, while there's an honest trade to be learnt, or brick-making to turn to.”
“不要害怕! 我们将不会成为您的作者,而这是一个值得学习的诚实交易,或者是要砖造的东西。”
I’ll base my examples on a React application that uses jsheatmap, a package I wrote for generating heat map data. The presentation of the heat map is done via a <table>
, where each cell’s background color is set to an RGB value that jsheatmap generates from a given set of input values.
我将基于使用jsheatmap的React应用程序创建示例, jsheatmap是我编写的用于生成热图数据的软件包。 热图的显示通过<table>
,其中每个单元格的背景色设置为jsheatmap从一组给定的输入值生成的RGB 值 。
const HeatMapTable = () => {const [players, setPlayers] = useState(2);const [suited, setSuited] = useState(false)const [ties, setTies] = useState(false)const [data, setData] = useState(getNewData(players, suited, ties))
There is a PlayersRow component that contains controls to allow the user to set the number of players needed to determine certain poker odds. It needs not only the initial value, but a setter to set new values. These properties (props) are players
and setPlayers
.
有一个PlayersRow组件,其中包含一些控件,允许用户设置确定某些扑克赔率所需的玩家数。 它不仅需要初始值,还需要设置器来设置新值。 这些属性(道具)的players
和setPlayers
。
One could use the time-honored technique of passing these props as explicit attributes when including the component in its container (HeatMapTable).
当将组件包含在其容器(HeatMapTable)中时,可以使用将这些道具作为显式属性传递的古老技术。
<Players players={players} setPlayers={setPlayers} />
Pretty basic stuff.
很基本的东西。
支撑简洁方式 (Props the concise way)
"Please, sir, I want some more."
“请,先生,我想要更多。”
In this case (as often happens), the variables used to hold the prop values often have the same names as the attribute names of the React components. This allows a more concise syntax to pass the props down to the child.
在这种情况下(经常发生),用于保存属性值的变量通常与React组件的属性名称具有相同的名称。 这允许更简洁的语法将道具传递给孩子。
In the example just shown, there are two props; you might have many more. One technique is to use an intermediate object to hold the props that are needed by the child component, then use the spread operator to “expand” the prop object into attribute values.
在刚刚显示的示例中,有两个道具。 您可能还有更多。 一种技术是使用中间对象来保存子组件所需的道具,然后使用传播运算符将道具对象“扩展”为属性值。
const props = {players, setPlayers, anotherProp, yetAnotherProp, etc};
<Players {...props} />
Which is equivalent to the much more verbose:
这相当于更为冗长:
<Players players={players} setPlayers={setPlayers} anotherProp={anotherProp} yetAnotherProp={yetAnotherProp} etc={etc} />
支撑简洁方式 (Props the conciser way)
“Alas! How few of Nature's faces are left alone to gladden us with their beauty!"
“唉! 剩下多少只大自然的面Kong让我们为它们的美丽而高兴!”
It turns out that the intermediate variable is not really needed, because you can just do this:
事实证明,实际上不需要中间变量,因为您可以这样做:
<Players {...{players, setPlayers, anotherProp, yetAnotherProp, etc}} />
Or even something that mixes intermediate objects with the spread operator:
甚至将中间对象与散布运算符混合在一起的东西:
const props = {anotherProp, yetAnotherProp, etc};
<Players {...{players, setPlayers, ...props}} />
As I said, not all that clever, it's just using the spread operator to spread an object declared inside the the JavaScript denoted by the outer {}
.
就像我说的那样,并不是很聪明,它只是使用split运算符来传播在用外部{}
表示JavaScript内部声明的对象。
So that's all there is! A little trick I find myself using ever more frequently.
这就是全部! 我发现自己经常使用一个小技巧。
翻译自: https://www.freecodecamp.org/news/yet-another-post-about-react-props/
react 渲染道具
相关文章:

高可用集群的概念
一:什么是高可用集群 高可用集群(High Availability Cluster,简称HA Cluster),是指以减少服务中断时间为目的得服务器集群技术。它通过保护用户得业务程序对外部间断提供的服务,把因为软件,硬件…

node.js 出现cannot find module ‘xxx‘ 解决办法
找不到模块的解决方案 : 把node_module整个文件夹删掉,然后npm clean cache,看下package.json里有没有express的依赖项,有的话直接npm install,没有的话 npm install express --save npm clean cache 如果不行的话用 npm cache clean

poj 1904 King's Quest
Kings Quest 题意:有N个王子和N个妹子;(1 < N < 2000)第i个王子喜欢Ki个妹子;(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚;问每一个王子可以有几种选择(在自己喜欢的妹子里面选),并输出可选…

数据预处理--噪声_为什么数据对您的业务很重要-以及如何处理数据
数据预处理--噪声YES! Data is extremely important for your business.是! 数据对您的业务极为重要。 A human body has five sensory organs, and each one transmits and receives information from every interaction every second. Today, scientists can det…

C++ template
(转自http://www.cnblogs.com/gw811/archive/2012/10/25/2738929.html) C模板 模板是C支持参数化多态的工具,使用模板可以使用户为类或者函数声明一种一般模式,使得类中的某些数据成员或者成员函数的参数、返回值取得任意类型。 模…

js根据经纬度取随机附近经纬度
实现功能: 小程序根据当前经纬度得出随机的附近经纬度显示在地图上做标记点,效果图 实现代码 // map.js var app getApp() var mymap ; var lat ; var long ; var that;function latlog(lat, lon, d 1,d23) {var angle Math.random(1, 360);var …

讽刺笑话_完全不讽刺的2019年网络设计指南
讽刺笑话I’ve written about how to design for the modern web before, way back in 2018. But the web moves forward quickly so those guidelines are already obsolete and outdated, as more modern conventions have become mainstream.早在2018年,我就已经…

模拟城市2.0
题目背景 博弈正在机房颓一个叫做《模拟城市2.0》的游戏。 2048年,经过不懈努力,博弈终于被组织委以重任,成为D市市委书记!他勤学好问,励精图治,很快把D市建设成富强民主文明和谐的美好城市。为了进一步深化…

bzoj:1221;vijos 1552 软件开发
Description 某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员每天提供一块消毒毛巾,…

u-charts 曲线图中间有部分没数据,导致点和点无法连成线的问题解决
解决曲线图或者折线图在两端中间没有数据时无法绘画成线的问题源码修改, 解决方案: 在数据之间填充假数据,并且创建一个和点的数据同级的 list 来验证是不是假数据,如果是假数据就不绘制点,是真数据才绘制点,达到点和点之间没数据但是能连线的效果 先看效果图: 数据格…

python构建json_如何使用Python构建JSON API
python构建jsonThe JSON API specification is a powerful way for enabling communication between client and server. It specifies the structure of the requests and responses sent between the two, using the JSON format.JSON API规范是启用客户端和服务器之间通信的…

样式集 - 自适应居中弹窗
效果图: 弹窗1代码 <!-- 答题正确弹窗 --><block v-if"answer_true_show"><view class"answer_true_bg"></view><view class"answer_true"><img class"true_bg_img" :src"qualifyi…

struts2中 ServletActionContext与ActionContext区别
1. ActionContext 在Struts2开发中,除了将请求参数自动设置到Action的字段中,我们往往也需要在Action里直接获取请求(Request)或会话(Session)的一些信息,甚至需要直接对JavaServlet Http的请求(HttpServletRequest),响应(HttpServletResponse)操作. 我们需要在Action中取得req…

[记录]calculate age based on date of birth
calculate age based on date of birth know one new webiste:eval.in run php code转载于:https://www.cnblogs.com/fsong/p/5190273.html

有抱负的Web开发人员应考虑的6件事
Becoming a web developer can be as challenging as working out every day.成为网络开发人员就像每天锻炼一样具有挑战性。 It’s important to know what it will take to succeed as a web developer.重要的是要知道要成为一名Web开发人员要取得成功。 Here are 6 things…

阿里云OSS上传图片实现流程
前置,在阿里云开通OSS对象储存。然后在下图文件管理配置文件储存目录和图中传输管理配置访问域名。 1.复制 uploadFileUtil 文件夹和 uploadFile.js 文件在 util 文件夹 2.在使用的页面 引入 uploadFile 效果图: 实现代码 <template><view c…

修改远程桌面连接3389端口号
修改注册表: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\Wds\Repwd\Tds\Tcp 键:PortNumber,以十进制显示:3389,修改成55555,保存刷新注册表。 HKEY_LOCAL_MACHINE\SYSTEM\Curre…

理解 : UDID、UUID、IDFA、IDFV
iOS获取设备唯一标识的各种方法?IDFA、IDFV、UDID分别是什么含义?iOS获取设备ID总结IDFA解释 关于UUID的理解 : 英文名称是:Universally Unique Identifier,翻译过来就是通用唯一标识符。 UUID是指在一台机器上生成的数字,它保证对…

推箱子2-向右推!_保持冷静,砍箱子-me脚
推箱子2-向右推!Hack The Box (HTB) is an online platform allowing you to test your penetration testing skills. It contains several challenges that are constantly updated. Some of them simulating real world scenarios and some of them leaning more towards a C…

H5面试题---介绍js的基本数据类型
js的基本数据类型 Undefined、Null、Boolean、Number、String 转载于:https://www.cnblogs.com/songchunmin/p/7789582.html

Node.js express 之mongoose 从异步回调函数返回值,类似于同步
http://my.oschina.net/antianlu/blog/187023转载于:https://www.cnblogs.com/cylblogs/p/5192314.html

小程序登录、用户信息相关接口调整说明
为优化用户的使用体验,平台将进行以下调整: 2021年2月23日起,若小程序已在微信开放平台进行绑定,则通过wx.login接口获取的登录凭证可直接换取unionID2021年4月13日后发布的小程序新版本,无法通过wx.getU…

小程序 reduce_使用Reduce制作的10个JavaScript实用程序功能
小程序 reduceThe multi-tool strikes again. 多功能工具再次触击。 In my last article I offered you a challenge to recreate well-known functions using reduce. This article will show you how some of them can be implemented, along with some extras! 在上一篇文章…

流媒体,hls
所谓流媒体是指采用流式传输的方式在Internet播放的媒体格式。流媒体又叫流式媒体,它是指商家用一个视频传送服务器把节目当成数据包发出,传送到网络上。用户通过解压设备对这些数据进行解压后,节目就会像发送前那样显示出来。流媒体…

uniapp实现页面左右滑动,上下滑动事件
实现代码: <view class"" touchstart"touchstart" touchend"touchend"> </view> data() {return {touchData: {}, //滑动事件数据} } methods: {touchstart(e) {this.touchData.clientX e.changedTouches[0].clientX;…

android逆向分析概述_Android存储概述
android逆向分析概述Storage is this thing we are all aware of, but always take for granted. Not long ago, every leap in storage capacity was incremental and appeared to be impossible. Nowadays, we don’t give a second thought when contemplating how much of …

小程序地图的使用笔记
这两天在看小程序的地图,写写笔记记录一下 小程序官方文档提供的方法 https://mp.weixin.qq.com/debug/wxadoc/dev/api/location.html 腾讯地图提供的jssdk http://lbs.qq.com/qqmap_wx_jssdk/qqmapwx.html 根据提示使用腾讯地图jssdk需要申请,在实例化的…

JS 实现可停顿的垂直滚动
1 var ScrollMiddle {2 Odiv:document.getElementById(comment), // 目标DOM 3 Oli: document.getElementById(comment).getElementsByTagName(li), 4 times:30, // 配置滚动时间 …

uniapp 上拉加载更多完整实现源码
直接上代码 <template><view class"searchList"><!-- 搜索框 --><Search></Search><img class"top_img" src"/static/image/dataDelivery.png" /><view class"menus p_r"><view class&…

todoist 无法登陆_通过构建Todoist克隆将您的React技能提升到一个新的水平
todoist 无法登陆In this intermediate React course from Karl Hadwen, you will learn how to create the popular Todoist application from scratch using React, Custom Hooks, Firebase & the React Testing Library. You will lean how to use SCSS to style the ap…