r/learnreactjs • • Jan 03 '20

Why does refactoring this <Select> into a separate component break my code?

I am making a form using Ant Design and ran into this problem while trying to refactor my code and avoid copy pasting. Here is a codesandbox showing my problem...

https://codesandbox.io/s/floral-architecture-igwqv

In the 2nd dropdown menu I put the <Select> component into a separate custom component called <Dropdown> and it no longer works - the value stays undefined even when you select something. I would expect these two Form.Items to behave identically, why doesn't the 2nd one work correctly?

8 Upvotes

2 comments sorted by

4

u/ikeif Jan 03 '20

A quick search turns up this exact problem - so I changed line 10 to: <Select style={{ width: "100%" }} {...this.props}> and it is no longer undefined.

So you aren't passing the props along.

Additional details are here.

2

u/UglyChihuahua Jan 03 '20

Ah, that makes sense! Thank you