Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react pass a class object as prop

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.

like image 452
Tzook Bar Noy Avatar asked Oct 16 '22 22:10

Tzook Bar Noy


1 Answers

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} />
like image 141
Shubham Khatri Avatar answered Oct 20 '22 00:10

Shubham Khatri