Skip to content

Feature request: Allow className to take an array #3138

Closed
@trotzig

Description

@trotzig

I often use components that both have an internal className as well as an optional className passed in as a prop. The way I've been implementing this is either through (A) a local array that I construct and serialize into the className prop, or (B) through using React.addons.classSet.

A:

var classNames = ['my-component'];
if (this.props.className) {
  classNames.push(this.props.className);
}
return <MyComponent className={classNames.join(' ')} />;

B:

var classes = {
  'my-component': true
};
if (this.props.className) {
  classes[this.props.className] = true;
}
return <MyComponent className={React.addons.classSet(classes)} />;

A is slightly less verbose, but harder to use if you have modifier classes as well (e.g. my-component--is-selected).

I'm suggesting allowing the className to be a string or an array. And if it is an array, React internals would compact and serialize the className array into a className string.

return <MyComponent className={['my-component', this.props.className]} />;

If this is a good idea, I could make an attempt at a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions