简体   繁体   English

react通过类对象作为prop

[英]react pass a class object as prop

I know you can pass plain objects and arrays in react props easily. 我知道您可以轻松地在React道具中传递普通对象和数组。

But my question is if its possible to pass a class object as well? 但是我的问题是是否还可以传递类对象?

I mean, I have this class: 我的意思是,我有这个课程:

class Something {
    constructor() {//data}
    method() {...}
    etc...
}

now I want to pass that object as a prop: 现在我想将该对象作为道具传递:

let sm1 = new Something();
<Component item={sm1} />

if you wonder why is simply because I have a big array of specific rules that I easily use the rules inside that class. 如果您想知道为什么仅仅是因为我有很多特定的规则,那么我很容易在该类中使用这些规则。

A class is an object in JavaScript and it makes no difference if you pass a class, an object or an array. class是JavaScript中的object ,如果您传递类,对象或数组,则没有任何区别。 You can pass the class instance without having any side-effects. 您可以传递类实例,而不会产生任何副作用。 Its just like a passing a function as a prop 就像传递函数作为道具一样

let sm1 = new Something();
<Component item={sm1} />

would be similar to 类似于

<Component handleChange={this.handleChange} />

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

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