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组件,其中包含一些控件,允许用户设置确定某些扑克赔率所需的玩家数。 它不仅需要初始值,还需要设置器来设置新值。 这些属性(道具)的playerssetPlayers

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 渲染道具

react 渲染道具_关于React道具的另一篇文章相关推荐

  1. react 渲染道具_在React中学习分解道具的基础

    react 渲染道具 by Evelyn Chan 通过伊芙琳·陈 在React中学习分解道具的基础 (Learn the basics of destructuring props in React ...

  2. react 最佳实践_最佳React教程

    react 最佳实践 React is a JavaScript library for building user interfaces. It was voted the most loved i ...

  3. react待办事项_使用React创建一个简单的待办应用

    react待办事项 You could be wondering what is so special about React; What we will do is pick up from a p ...

  4. mobx在react中应用_借助React Native Elements,Jest和MobX MST可以轻松实现现实世界中的ReactNative应用...

    mobx在react中应用 by Qaiser Abbas 由Qaiser Abbas 借助React Native Elements,Jest和MobX MST可以轻松实现现实世界中的ReactNa ...

  5. react实现汉堡_利用 React 高阶组件实现一个面包屑导航

    什么是 React 高阶组件 React 高阶组件就是以高阶函数的方式包裹需要修饰的 React 组件,并返回处理完成后的 React 组件.React 高阶组件在 React 生态中使用的非常频繁, ...

  6. react 组件构建_为React构建星级评定组件

    react 组件构建 Who doesn't love beer? When you drink a great beer you want to tell someone. You definite ...

  7. 逆战选择服务器后显示器,逆战自选道具_逆战自选道具如何选择 为你带来详尽解答_52PK逆战...

    小伙伴们在开启自选靶场之后会在六种价值度极高的道具中选择其中的二种加入到十二种合成区域之中.不少玩家还不知道逆战自选道具如何选择,一起来看看今天的文章吧! [自选道具合成选择一览] 在本期自选道具合成 ...

  8. 如何开发React Native 原生模块(Native Modules)?看完这篇文章就够了(Android)

    期待已久的新课上线啦!解锁React Native开发新姿势,一网打尽React Native最新与最热技术,点我Get!!! 前言 一直想写一下我在React Native原生模块封装方面的一些经验 ...

  9. react 组件构建_使用React Spring和Tinycolor构建色彩丰富的弹性组件

    react 组件构建 Recently, I decided to build a web application to allow designers and developers to gener ...

最新文章

  1. python之⾯向对象基础
  2. 2.1 操作系统的类型与结构
  3. python语言教程-Python语言教程手册
  4. exe4j打包exe_Java日常实用技巧之程序打包为可执行文件
  5. iOS8开发~UI布局(二)storyboard中autolayout和size class的使用详解
  6. sublime安装Codecs33
  7. 抢答网页PHP,[抢答]抢答环节已结束,相关答案已封箱 - 薅羊毛(Coupon)版 - 北大未名BBS...
  8. MinGW与MSVC编译的区别
  9. JavaScript-获得和设置表单的值
  10. [编程手记小技巧]自定义调试信息宏及gcc技巧
  11. php笔记之-laravel-Redis hash
  12. 如何高效的将 DataReader 转成 List<T> ?
  13. cocos2d-x3.0中数据类型vector,map、value
  14. java 同步和异步_知道什么叫同步和异步吗?
  15. 计算机网络物理防护,计算机网络的物理安全
  16. java 生成pdf 下载_java生成PDF,并下载到本地
  17. 分类信息网站模板(ICP许可证)已通过审核-两套样式模板
  18. 9V降压5V低功耗恒压稳压芯片,大电流3A方案和LDO
  19. 项目管理中团队合作有多重要
  20. CSS 实现文字渐变色

热门文章

  1. vue中把props中的值赋值给data
  2. ssm 实现房屋租赁系统
  3. 操作系统导论部分章节习题
  4. threadlocal使用场景_深入剖析ThreadLocal
  5. 生成公钥链接github
  6. iOS自动签名打包(xcodebuild)----常用
  7. 通往SQL Server复制的阶梯:一级- SQL服务器复制介绍
  8. mysql dba系统学习(19)配置mysql+lvs+keeplived实现Mysql读操作的负载均衡
  9. spring中@value注解需要注意
  10. 深入Java虚拟机之虚拟机体系结构