Write More Reusable React Components with Composable APIs

Share this video with your friends

Send Tweet

To make more composable React components, you can define common APIs for similar component types.

Dean
Dean
~ 10 years ago

Hey - What would you suggest that if we wanted to reuse "NumInput" but "add" some additional props or whatever, is there a way to "extend" it. Because we might want to use NumInput , but might need something additional so its usage isn't so particular.

Joe Maddalone
Joe Maddalone(instructor)
~ 10 years ago

There's no clear method in React for extending an existing component, but mixins are a great place to start.

You could also wrap the component you want to inherit from and call it's methods using a ref.

<code> var App = react.createClass({ method: function(){ return this.refs.comp.method(); }, render: function(){ return &lt;InheritFromThisComponent ref="comp" /> } }) </code>
Faria Chowdhury
Faria Chowdhury
~ 10 years ago

In your example, the input is an uncontrolled component because you use defaultvalue, instead of a value, is that correct? Does the code need to be changed if I make it a controlled component instead?

leonardo
leonardo
~ 9 years ago

Inhale and exhale, you are teaching man!

Abhishek
Abhishek
~ 9 years ago

I am using React version 0.14.0 on a Mac OSX

When I type this code in, it is functional and I am getting an error of "Uncaught TypeError: React.findDOMNode is not a function"

I understand React.findDOMNode is introduced in the 0.13.0 release of React. Why is this error being thrown? Did it get deprecated in 0.14.0?

Thanks

Joe Maddalone
Joe Maddalone(instructor)
~ 9 years ago

In React v0.14 some methods like findDOMNode have been moved to a separate package.

Include https://fb.me/react-dom-0.14.0.js in your application and use ReactDOM.findDOMNode, ReactDOM.render, and ReactDOM. unmountComponentAtNode where applicable.

Mitesh
Mitesh
~ 7 years ago

Hi Joe,In the beginning of this lecture you have mentioned that you have created all the input range components using refs in a separate lecture,I am unable to find the lecture in this series.If possible can you please provide me a link to that lecture.

Thanks

Maria
Maria
~ 7 years ago

Same here. And there is nothing in the course repo either. Thanks!

Maria
Maria
~ 7 years ago

Hi Mitesh, even though I didn't find the original code for the various inputs, I did manage to get the code that we start with here to work. When I push it to github, I will share the link here. Perhaps this will help?

Maria
Maria
~ 7 years ago

Mitesh (and anyone else who is interested), here is the link of the commit containing the original code re various inputs/input refs: https://github.com/interglobalmedia/react-basics/commit/ab93964c44e393cc11e98584915ac79b6e39f85f Hope this helps!