admin管理员组文章数量:1332403
I know you can pass plain objects and arrays in react props easily.
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.
I know you can pass plain objects and arrays in react props easily.
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.
Share Improve this question asked Feb 22, 2018 at 12:27 Tzook Bar NoyTzook Bar Noy 11.7k14 gold badges57 silver badges85 bronze badges 4- 2 Why not???, Everything is an object in javascript. did you give it a try. – Shubham Khatri Commented Feb 22, 2018 at 12:30
- There is no difference between class object and plain object. – Agney Commented Feb 22, 2018 at 12:31
- Maybe I'm scared as I need to be functional like in redux... – Tzook Bar Noy Commented Feb 22, 2018 at 12:32
- hope this help, stackoverflow./questions/42510874/… – Jayavel Commented Feb 22, 2018 at 12:44
1 Answer
Reset to default 5A class
is an object
in JavaScript and it makes no difference if you pass a class, an object or an array. 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} />
本文标签: javascriptreact pass a class object as propStack Overflow
版权声明:本文标题:javascript - react pass a class object as prop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742278433a2445639.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论