Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a React component?

Let's say there's a React component that I like, but want to modify. For this example, we'll use Material UI's LinearProgress. I want to make a clickable seek bar out of it.

class SeekBar extends LinearProgress {   componentDidMount() {     super.componentDidMount();     console.log('my stuff here');   } } 

But I feel like I might be very limited as to what I can do as far as changing what render returns. Maybe I'm going about this all wrong, though. If I like a particular React component such as a Material UI component, what is a good, reusable way to customize its look and functionality and make it my own?

like image 395
ffxsam Avatar asked Feb 28 '16 00:02

ffxsam


People also ask

Can you extend a component React?

Using the extends keyword, you can allow the current component to access all the component's properties, including the function, and trigger it from the child component. This example creates one component called ParentClass. jsx. ParentClass extends the component from React as React.

How do you extend a functional component in React?

A simple approach is to move your helpful functions outside of the function components, so that they can be used by multiple components. These could be in a utilities file that you import from Home. jsx and APIHome. jsx , but here I've coded them as if they're all in one file for simplicity.

What is the extension of React component?

It is called JSX, and it is a syntax extension to JavaScript. We recommend using it with React to describe what the UI should look like. JSX may remind you of a template language, but it comes with the full power of JavaScript. JSX produces React “elements”.

Why do we Extends component in React?

Extending Component will give you access to functions like componentDidMount , componentDidUpdate , componentWillUnmount and render . Show activity on this post. if you extend React. Component class , you can use the life cycle of the Component which can be used for mounting and unmounting.


2 Answers

In the ES6/JSX/React world, you can extend component behaviors and values in many ways. I'd recommend one of the following, considering that you use Material UI.

First case:

You have two components that extend the Component from React:

class ExampleComponent extends React.Component {   render () {     return(       <div>         <button {...this.props}>           Click me!         </button>       </div>     )   } }  class RenderComponent extends React.Component {   clickHandler () {     console.log('Click fired!')   }    render () {     return(       <ExampleComponent onClick={this.clickHandler.bind(this)} />     )   } } 

In that example, onClick is passed via props inside the rendered ExampleComponent. Example here.

Second case:

This is similar on how Material UI extends their own components:

class ExampleComponent extends React.Component {   clickHandler () {     console.log('Click fired!')   } }  class RenderComponent extends ExampleComponent {   render () {     return(       <div>         <button onClick={this.clickHandler.bind(this)}>           Click me!         </button>       </div>     )   }   }  

In this example, you have one component that extends Component from React but only has event methods. Then, you extend this component and render your own with the extended behavior. Here is a live example.

Hope it helps!

like image 129
Inacio Schweller Avatar answered Sep 20 '22 08:09

Inacio Schweller


One way is:

export default class Seekbar extends React.Component{     // perform any modification     render(){       return <LinearProgress ...changes/>    } } 

Another way is a Higher Order Component, HOC. Here's a great blog with more info: http://natpryce.com/articles/000814.html

I think with Material-UI your best bet would be to wrap it and make any modifications you'd like. Unfortunately this project is very tightly coupled thanks to things like inline styles and their thememanager so taking a single component out may be difficult. HOC would be better for things like sharing some smart functionality across components, like passing a "theme" prop automatically to components.

like image 23
Tucker Avatar answered Sep 22 '22 08:09

Tucker