简体   繁体   English

如何将具有大量属性的对象作为prop传递给React Element?

[英]How to pass as prop for a React Element an object that has a huge number of properties?

Please, can you help me to understand what is the corresponding JSX syntax to thi React element? 拜托,您能帮我了解一下React元素对应的JSX语法是什么吗?

const data = {
   className: "myEl",
   "data-numb": 3,
   "data-created": 191017
   //following other 20 attributes
}
React.createElement("div",data,"test");

I guess you will not going to write this element in JSX syntax in this way 我想您不会以这种方式用JSX语法编写此元素

<div className="myEl" data-numb=3 data-created=191037 ....the other 20 attributes.... />

So, what's the ideal way to pass as prop for the React Element an object that has a huge number of properties? 那么,传递给具有大量属性的对象作为React Element的理想方式是什么?

As far as I understand you are asking for this: 据我了解,您要求这样做:

const data = {
   className: "myEl",
   "data-numb": 3,
   "data-created": 191017
   //following other 20 attributes
}

<div {...data}/>

Why not use a data store solution like redux? 为什么不使用像redux这样的数据存储解决方案? Then for each component that needed any data from the store, you could just pass whatever you wanted from state to the component via mapStateToProps. 然后,对于需要从存储中获取任何数据的每个组件,您都可以通过mapStateToProps将所需的状态从状态传递到该组件。 This will make things much easier for you in the long run, especially since it sounds like you have a lot of data you are working with. 从长远来看,这将使您的工作变得容易得多,特别是因为听起来您正在使用大量数据。 You can use Alexander's answer for now, but if you aren't using a data management solution yet, you are going to really want one before long. 您现在可以使用Alexander的答案,但是如果您还没有使用数据管理解决方案,那么您不久就会真正想要一个。 Combine this answer with Alexander's where you pass all your data in a single object, and I think your life will be much easier. 将这个答案与Alexander's结合起来,您可以在一个对象中传递所有数据,我认为您的生活会更加轻松。 If you are not familiar with redux yet, definitely check it out at: http://redux.js.org/ 如果您还不熟悉redux,请一定通过以下网址进行检查: http : //redux.js.org/

The mapStateToProps function is part of the react-redux library, which is used to connect react to the redux store. mapStateToProps函数是react-redux库的一部分,该库用于将react连接到redux存储。 This article here is a great help if you are trying to wrap your head around what its actually doing, and how you should use redux with your react application. 这篇文章在这里是一个很大的帮助,如果你想环绕它的是什么实际上做你的头,你应该如何使用终极版与反应中的应用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM