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} />