superZcyan
superZcyan

Reputation: 39

Create a method for passing props to components in REACT

Is it possible to create a method that contains passing props that will be use for multiple components.

passingPropsMethod(){
        return(
           something={this.state.something}
           something2={this.state.something2}
        )
    }

Tried this code but has "Unreachable code detected".

What would be the best approach to achieve this?

Upvotes: 1

Views: 55

Answers (1)

Armando K.
Armando K.

Reputation: 504

You should return an object from the function instead:

passingPropsMethod() {
  return {
     something: this.state.something,
     something2: this.state.something2
  }
}

And pass it to your component like this:

<Component {...passsingPropsMethod()} />

or

const props = passsingPropsMethod()
<Component {...props} />

Upvotes: 2

Related Questions