0

I asked another question that involved inheritance among React components. One answer stated that inheritance is counter-indicated for React components. I can construct examples where inheritance could add clarity and reduce code. Is inheritance an anti-pattern for React components? Why or why not?

9
  • What do you mean by inheritance? As in creating a class-based component and then have another component extend that? Commented Mar 24, 2019 at 20:20
  • Yes - Javascript (ES6) native class inheritance with extends Commented Mar 24, 2019 at 20:20
  • 2
    At Facebook, we use React in thousands of components, and we haven’t found any use cases where we would recommend creating component inheritance hierarchies. from: reactjs.org/docs/composition-vs-inheritance.html Commented Mar 24, 2019 at 20:23
  • 1
    @SakoBu That link is very helpful. Thank you. Care to make it an official answer? Commented Mar 24, 2019 at 20:31
  • 1
    You will find this post by Dan Abramov, one of the co-creators of React very enlightening Commented Mar 24, 2019 at 20:32

1 Answer 1

2

A common question from developers coming from an OOP background... Here is a link from the official docs that clearly explains why composition is preferred over inheritance:

https://reactjs.org/docs/composition-vs-inheritance.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.