Cazineer
Cazineer

Reputation: 2405

Should I use ProptTypes for props I pass through a React component?

Should I use PropTypes for props I pass through a React component to a child component, but don't actually use within component itself?

In the following example, Component2 would have PropTypes for the props received from Component1, even though Component3 is the component using the props.

<Component1 someValue=someValue">
  <Component2 {...props}>
    <Component3>
      <div>Hello World! {someValue}</div>
    </Component3>
  </Component2>
</Component1>

Upvotes: 0

Views: 218

Answers (2)

Tomasz Mularczyk
Tomasz Mularczyk

Reputation: 36179

In my opinion, its enough to declare propTypes for props that Parent component is using and if rest of props is just passed through, then it's Children component responsibility to validate them. This way you avoid repeating yourself and still get error validation.

Upvotes: 0

Akshat Gupta
Akshat Gupta

Reputation: 2082

you can actually write something like this. It makes the code more readable and instantly tells some other developer about your intent

MyComponent.propTypes = {
 someValue: PropTypes.string,
 ...Component2.propTypes
}

Upvotes: 1

Related Questions