P
P
p3trukh1n2019-05-29 22:30:05
JavaScript
p3trukh1n, 2019-05-29 22:30:05

Is it possible to somehow group elements with the same parameters in React?

There are multiple JSX elements with the same parameter part:

<NavLink to='/stats' activeClass="active" activeStyle={{fontWeight: "bold"}}>Stats</NavLink>
<NavLink to='/home' activeClass="active" activeStyle={{fontWeight: "bold"}}>Home</NavLink>

It is cumbersome to use the same activeClass and activeStyle in all links (there are more than 2 of them), how can you shorten the code so as not to write the same parameters for each element?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2019-05-29
@p3trukh1n

const SuperLink = (props) => <NavLink activeClass="active" activeStyle={{fontWeight: "bold"}} to={props.to}>{props.text}</NavLink>

....

<SuperLink to="/stats" text="Stats"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question