admin管理员组文章数量:1025791
const Header = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired,
},
render() {
return (
<li className={this.context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={this.context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
},
});
I use eslint-config-airbnb to check the above code and it show me an error msg like Component should be written as a pure function .
So how to change the above ponent to pure function?
Thanks for your help.
const Header = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired,
},
render() {
return (
<li className={this.context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={this.context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
},
});
I use eslint-config-airbnb to check the above code and it show me an error msg like Component should be written as a pure function .
So how to change the above ponent to pure function?
Thanks for your help.
Share Improve this question asked Jun 2, 2016 at 17:24 Veljko SimicVeljko Simic 711 silver badge5 bronze badges3 Answers
Reset to default 4When you have a "dumb" ponent (no internal state, lifecycle methods, etc.), you may write it as a simple javascript function as opposed to using React.CreateClass
or extending React.Component
.
Check out the docs here for general information on pure functions as ponents and here for information specific to context
.
So in your case:
function Header(context) {
return (
<li className={context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
}
Header.contextTypes = {
router: React.PropTypes.object.isRequired,
}
You could also try the ES6 way:
const Header = (context) => (
<li className={context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
Header.contextTypes = {
router: React.PropTypes.object.isRequired,
}
export default Header;
I use some fake props to bypass this error:
export default class NavItem extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
const { router } = this.context;
const { index, onlyActiveOnIndex, to, children } = this.props;
const isActive = router.isActive(to, onlyActiveOnIndex);
const LinkComponent = index ? IndexLink : Link;
return (
<li className={isActive ? 'active' : ''}>
<LinkComponent to={to}>{children}</LinkComponent>
</li>
);
}
}
NavItem.propTypes = {
children: React.PropTypes.node.isRequired,
index: React.PropTypes.bool,
onlyActiveOnIndex: React.PropTypes.bool,
to: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object,
]).isRequired,
};
NavItem.contextTypes = {
router: React.PropTypes.object,
};
const Header = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired,
},
render() {
return (
<li className={this.context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={this.context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
},
});
I use eslint-config-airbnb to check the above code and it show me an error msg like Component should be written as a pure function .
So how to change the above ponent to pure function?
Thanks for your help.
const Header = React.createClass({
contextTypes: {
router: React.PropTypes.object.isRequired,
},
render() {
return (
<li className={this.context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={this.context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
},
});
I use eslint-config-airbnb to check the above code and it show me an error msg like Component should be written as a pure function .
So how to change the above ponent to pure function?
Thanks for your help.
Share Improve this question asked Jun 2, 2016 at 17:24 Veljko SimicVeljko Simic 711 silver badge5 bronze badges3 Answers
Reset to default 4When you have a "dumb" ponent (no internal state, lifecycle methods, etc.), you may write it as a simple javascript function as opposed to using React.CreateClass
or extending React.Component
.
Check out the docs here for general information on pure functions as ponents and here for information specific to context
.
So in your case:
function Header(context) {
return (
<li className={context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
}
Header.contextTypes = {
router: React.PropTypes.object.isRequired,
}
You could also try the ES6 way:
const Header = (context) => (
<li className={context.router.isActive('a') ? 'active' : ''}>
<Link to="/a/">A</Link>
</li>
<li className={context.router.isActive('b') ? 'active' : ''}>
<Link to="/b/">B</Link>
</li>
);
Header.contextTypes = {
router: React.PropTypes.object.isRequired,
}
export default Header;
I use some fake props to bypass this error:
export default class NavItem extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
const { router } = this.context;
const { index, onlyActiveOnIndex, to, children } = this.props;
const isActive = router.isActive(to, onlyActiveOnIndex);
const LinkComponent = index ? IndexLink : Link;
return (
<li className={isActive ? 'active' : ''}>
<LinkComponent to={to}>{children}</LinkComponent>
</li>
);
}
}
NavItem.propTypes = {
children: React.PropTypes.node.isRequired,
index: React.PropTypes.bool,
onlyActiveOnIndex: React.PropTypes.bool,
to: React.PropTypes.oneOfType([
React.PropTypes.string,
React.PropTypes.object,
]).isRequired,
};
NavItem.contextTypes = {
router: React.PropTypes.object,
};
版权声明:本文标题:javascript - how to solve Component should be written as a pure function error in eslint-react? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745639501a2160656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论