Write More Reusable React Components with Composable APIs

InstructorJoe Maddalone

Share this video with your friends

Send Tweet

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

Dean
~ 6 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 Maddaloneinstructor
~ 6 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
~ 6 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
~ 5 years ago

Inhale and exhale, you are teaching man!

Abhishek
~ 5 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 Maddaloneinstructor
~ 5 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
~ 4 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
~ 4 years ago

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

Maria
~ 4 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
~ 4 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!