Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.cloneElement in List performance

I have doubts about React.cloneElement in List. Is that something that we should avoid doing or not, if we have lots of elements in list? Does React.cloneElement makes unnecessary re-renders that could be avoided?

My component:

...
      render() {
        const { items, classes, children } = this.props;
        const { expanded } = this.state;
        return (
          <List className={classes.cssRoot}>
            <Scrollbars
              style={classes.cssScrollBar}
              renderThumbVertical={this.renderThumb}
            >
              {items.map((item, index) => {
                return (
                  <ListItem key={item.id} className={classes.cssListItemRoot}>
                    {React.Children.map(children, child =>
                      React.cloneElement(child, {
                        id: item.id,
                        name: `Item nummber ${index}`,
                        handleChange: this.handleChange,
                        isExpanded: expanded === item.id
                      })
                    )}
                  </ListItem>
                );
              })}
            </Scrollbars>
          </List>
        );
      }
...
like image 575
rpk Avatar asked Feb 28 '19 09:02

rpk


People also ask

Should you use React cloneElement?

React. cloneElement() is useful when you want to add or modify the props of a parent component's children while avoiding unnecessary duplicate code.

What is the difference between createElement and cloneElement?

createElement is the code that JSX gets compiled or converted into and is used by reacting to create elements. cloneElement is used for cloning elements and passing them new props. This method is used to describe how the User Interface looks. This method is used to manipulate the elements.

What is React cloneElement and the difference with this props children?

The React. cloneElement() function returns a copy of a specified element. Additional props and children can be passed on in the function. You would use this function when a parent component wants to add or modify the prop(s) of its children.

How do I clone a div in react JS?

We can use React. cloneElement() method when a parent component wants to add or modify the props of its children. The React. cloneElement() function creates a clone of a given element, and we can also pass props and children in the function.


1 Answers

Seeing React.cloneElement in the middle of some JSX can look a little scary and unfamiliar, but it is very benign from a performance standpoint. The thing to realize is that JSX gets transformed into calls to React.createElement which just returns an object. React.cloneElement is just copying that object and allowing you to modify props in the process. The resulting object will look no different to React than the original object created via JSX aside from the prop changes, and it has no harmful effects as far as causing extra renders. The performance impacts are of no more concern than if you had a function that was transforming some data by taking in an array of objects and creating a new array with copies of those objects with an additional property.

Material-UI leverages React.cloneElement internally in a number of places to add in additional props to children such as in Radio.js.

like image 143
Ryan Cogswell Avatar answered Oct 24 '22 02:10

Ryan Cogswell