In my opinion, it will be much easier to reuse Link
in the MyLink MyLink
:
import React from 'react'; import {Link} from 'react-router'; import styles from './CommonStyles'; function MyLink(props) { return ( <Link {...props} style={styles.title} activeStyle={styles.active} > {props.children} </Link> ); } export default MyLink;
A pure function is used, since except for the render from this component, nothing more is needed.
Attributes style
and activeStyle
are set to default values.
{...props}
needed in order from the parent to pass additional attributes directly to Link
, for example, such as "to"
or any other attributes supported by the Link
component.
{props.children}
- insert MyLink
child elements inside Link
.
MyLink
can use MyLink
like this:
<MyLink to="/"> <p>Это какой-то текст ссылки</p> </MyLink>
If you also want to supplement / override the default Link
styles from the parent, this can be done through their extension:
function MyLink(props) { return ( <Link {...props} style={Object.assign({}, styles.title, props.style)} activeStyle={Object.assign({}, styles.active, props.activeStyle)} > {props.children} </Link> ); }
And use:
<MyLink to="/" style={{ color: 'red' }} activeStyle={{ color: 'blue' }} > <p>Это какой-то текст ссылки</p> </MyLink>
The styles will then be merged, not completely rewritten.
In ECMAScript 6 style, it looks like this:
import React from 'react'; import {Link} from 'react-router'; import styles from './CommonStyles'; class MyLink extends React.Component { constructor(props) { super(props); } render() { return ( <Link {...this.props} style={styles.title} activeStyle={styles.active} > {this.props.children} </Link> ); } } export default MyLink;