<Route path="/:user" component={Profile} />
this.props.match.params.user
) значением пользоваться и все.export function findUserById(id) => {
// ищем по id элемент в массиве и возвращаем его
}
.highlighted {
color: red;
font-weight: bold;
font-size: 1.5em;
}
class App extends React.Component {
state = {
items: [ 'aaa', 'aa1', 'ab1', 'zzz', '00!', 'lorem ipsum dolor...' ],
search: '',
}
render() {
const { items, search } = this.state;
const regex = RegExp(search, 'g');
const replacement = '<span class="highlighted">$&</span>';
return (
<div>
<input value={search} onChange={e => this.setState({ search: e.target.value })} />
{search &&
<ul>
{items.filter(n => n.includes(search)).map(n =>
<li dangerouslySetInnerHTML={{ __html: n.replace(regex, replacement) }} />
)}
</ul>}
</div>
);
}
}
const Input = ({ label, ...props }) => (
<div>
<label>
{label}
<input {...props} />
</label>
</div>
);
class App extends React.Component {
state = {
from: 'Москва',
to: 'Питер',
}
onChange = ({ target: { value, name } }) => {
this.setState(() => ({
[name]: value,
}));
}
swap = () => {
this.setState(({ from, to }) => ({
from: to,
to: from,
}));
}
render() {
return (
<div>
<button onClick={this.swap}>swap</button>
<Input label="откуда" value={this.state.from} name="from" onChange={this.onChange} />
<Input label="куда" value={this.state.to} name="to" onChange={this.onChange} />
</div>
);
}
}