Pachu Pachu - 2 months ago 11
React JSX Question

React - adding class to children components

im using react and I have a component that simply needs to render the children and add them a class according to a condition.
Whats the best way of doing it?

Answer

I figured it out a week ago, this is what I wanted :

export default class ContainerComponent extends React.Component {
    constructor(props) {
        super(props);

        this.modifyChildren = this.modifyChildren.bind(this);
    }

    modifyChildren(child) {
        const className = classNames(
            child.props.className,
            {...otherClassses}
        );

        const props = {
            className
        };

        return React.cloneElement(child, props);
    }
    render() {
        return (<div>
            {React.Children.map(this.props.children, this.modifyChildren(child))}
        </div>);
    }
}