Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link: Make active class configurable #86

Closed
ryanflorence opened this issue Jul 17, 2014 · 6 comments
Closed

Link: Make active class configurable #86

ryanflorence opened this issue Jul 17, 2014 · 6 comments

Comments

@ryanflorence
Copy link
Member

Link.activeClassName = "somethingElse"

and

<Link activeClassName="somethingElse"/>

@sophiebits
Copy link
Contributor

Link.activeClassName sounds like a bad idea to me personally because it means that if you have a third-party component that has its own styles and uses react-nested-router then you could break it by changing this global config, which seems undesirable. If you want to change activeClassName everywhere in your app then you could make a MyLink wrapper around it that specifies activeClassName and passes the other props down verbatim (and use that everywhere instead of Link).

@mjackson
Copy link
Member

Agree with @spicyj. It's situations like these that transferPropsTo was made for.

var CustomLink = React.createClass({
  getDefaultProps: function () {
    return { activeClassName: 'custom-active-classname' };
  },
  render: function () {
    return this.transferPropsTo(
      <Link>{this.props.children}</Link>
    );
  }
});

@ryanflorence
Copy link
Member Author

just looked at our code and didn't realize we had that as a default prop, fantastic.

@christianvuerings
Copy link

@mjackson @ryanflorence It looks like transferPropsTo has been removed from React, any suggestions on how to do this in a different way?

Found some good docs at https://gist.github.com/sebmarkbage/a6e220b7097eb3c79ab7 but wasn't able to make the example (overwriting activeClassName for CustomLink) work yet.

@sophiebits
Copy link
Contributor

This is equivalent to what @mjackson said:

var CustomLink = React.createClass({
  getDefaultProps: function () {
    return { activeClassName: 'custom-active-classname' };
  },
  render: function () {
    return <Link {...this.props}>{this.props.children}</Link>;
  }
});

If you want activeClassName to override what was passed in, do:

var CustomLink = React.createClass({
  render: function () {
    return (
      <Link {...this.props} activeClassName="custom-active-classname">
        {this.props.children}
      </Link>
    );
  }
});

@christianvuerings
Copy link

@spicyj Thanks, that works beautifully!

christianvuerings added a commit to christianvuerings/pentire that referenced this issue Aug 19, 2015
* Router functionality
* Custom active class - thanks to @spicyj - remix-run/react-router#86
* Update deps
* Namespace CSS
* Remove learn modules
@lock lock bot locked as resolved and limited conversation to collaborators Jan 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants