最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - react pass a class object as prop - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

A 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} />
发布评论

评论列表(0)

  1. 暂无评论