首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Radium不能与React路由器IndexLink组件一起使用

Radium不能与React路由器IndexLink组件一起使用
EN

Stack Overflow用户
提问于 2016-07-15 00:57:27
回答 1查看 299关注 0票数 0

Radium不能与React路由器IndexLink组件一起使用。我使用了FAQ's method,但这并不能解决问题。

下面是我的代码:

代码语言:javascript
复制
import React, {PropTypes} from 'react';
import {IndexLink} from 'react-router';
import {deepPurple500, deepPurple300, grey600} from 'material-ui/styles/colors';
import radium from 'radium';

import {default as rem} from 'helpers/calculateRem';

const DecoratedIndexLink = radium(IndexLink);

/**
 * Link component.
 *
 * @param {Object} style
 * @param {String} to
 * @param {String} label
 * @param {Boolean} secondary
 */
function Link({style, to, label, secondary}) {
  const defaultStyle = {
    textDecoration: 'none',
    color: secondary ? grey600 : deepPurple500,
    borderBottomWidth: rem(1),
    borderBottomStyle: 'solid',
    borderColor: secondary ? grey600 : deepPurple500,
    ':hover': {
      color: deepPurple300
    }
  };

  return <DecoratedIndexLink style={{...style, ...defaultStyle}} to={to}>{label}</DecoratedIndexLink>;
}

Link.prototype.propTypes = {
  style: PropTypes.obj,
  to: PropTypes.string,
  label: PropTypes.string,
  secondary: PropTypes.bool
};

export default radium(Link);

我用Radium装饰了export default,但无论有没有它,都不会有任何变化。我甚至尝试用像button这样的DOM元素来替换IndexLink,所以我猜它完全与定制组件有关。

对这个案子有什么提示吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-22 04:55:35

代码语言:javascript
复制
import React, {PropTypes} from 'react'; 
import {Link} from 'react-router';
import {deepPurple500, deepPurple300, grey600} from 'material-ui/styles/colors';
import radium from 'radium';

import {default as rem} from 'helpers/calculateRem';

const DecoratedLink = radium(Link);

function Link({style, to, label, secondary) {
  const defaultStyle = {
    textDecoration: 'none',
    color: secondary ? grey600 : deepPurple500,
    borderBottomWidth: rem(1),
    borderBottomStyle: 'solid',
    borderColor: secondary ? grey600 : deepPurple500,
    ':hover': {
      color: deepPurple300
    }
  };

  return (
    <DecoratedLink style={[style, defaultStyle]} to={to} onlyActiveOnIndex>
      {label}
    </DecoratedLink>;
  );
}

Link.prototype.propTypes = {
  style: PropTypes.obj,
  to: PropTypes.string,
  label: PropTypes.string,
  secondary: PropTypes.bool
};

export default radium(Link);

如常见问题解答所示,Radium不能影响react组件中的自定义非DOM元素的样式。这意味着在导出时使用Radium装饰组件不会对自定义元素(如react-router的LinkIndexLink )产生任何影响。

Radium确实提供了一种适用于许多自定义元素的变通方法-将自定义元素包装在Radium中,例如它们的示例:Link = Radium(Link);。虽然这适用于react-router的Link元素,但它不适用于IndexLink。这是因为IndexLink只返回一个带有属性onlyActiveOnIndex的Link元素;Radium包装在IndexLink周围,但没有包装在它返回的Link元素周围。

由于在链接周围包装半径是无效的,所以在链接周围包装半径并传递onlyActiveOnIndex道具到其中。<Link {...props} onlyActiveOnIndex />的功能应该与<IndexLink {...props} />完全相同,并且在与Radium一起包装时也会正常工作。

onlyActiveOnIndex文档:https://github.com/jaredly/react-router-1/blob/6fae746355e2679b12518c798d3ef0e28a5be955/docs/API.md#onlyactiveonindex

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38380049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档