this.state ={url:url}
Так state обновлять нельзя.const MainLayout = ({ children }) => (
<Wrapper>
<Header />
<Content>
{children}
</Content>
<Footer />
</Wrapper>
);
<Switch>
<Route exact path="/" component={Main} />
{/* остальные роуты */}
</Switch>
const Main = () => (
<MainLayout>
{/* тут содержимое страницы */}
</MainLayout>
);
render() {
const { me, project, users } = this.props;
const isMeModerator = isModerator(me);
const isMeCreator = me.id === project.creator_id;
const shouldShowSearchForm = isMeModerator || isMeCreator;
return (
<div>
{shouldShowSearchForm && <Search />}
<ul>
{users.map(user => {
const isUserCreator = user.id === project.creator_id;
const shouldShowDeleteButton = (isMeModerator || isMeCreator) && !isUserCreator;
return (
<li key={user.id}>
{user.name}
{shouldShowDeleteButton && <Button>Delete</Delete>}
{isUserCreator && <CreatorLabel />}
</li>
);
}}
</ul>
</div>
);
}
<PermissionsChecker requiredPermissions={permissions}>
<SomeComponent />
</PermissionsChecker>
Как правильно использовать this.props.children?
import React from 'react';
import ReactDOM from 'react-dom';
const SomeComponent = ({ name }) => (
<span>{name}</span>
);
const SomeContainer = ({ children }) => (
<div>{children}</div>
);
const Example = () => (
<SomeContainer>
<SomeComponent name="John" />
</SomeContainer>
);
ReactDOM.render(
<Example />,
document.getElementById('root');
);
<div id="root">
<div>
<span>John</span>
</div>
</div>
import { Link, withRouter } from 'react-router-dom';
const Example = ({ location }) => (
<ul>
<li className={location.pathname === '/' ? 'active' : ''}>
<Link to="/">
<u>Главная</u>
</Link>
</li>
<li className={location.pathname === '/about' ? 'active' : ''}>
<Link to="/about">
<u>О нас</u>
</Link>
</li>
</ul>
);
export default withRouter(Example);
<Switch>
<Route path="/(en|ru|de|es|it|jp)" component={Routes} />
<Redirect to={`/${currentLocale.code}${location.pathname}`} />
</Switch>
<Switch>
<Route exact path={match.path} component={Main} />
<Route path={`${match.path}/other`} component={Other} />
<Redirect to={match.url} />
</Switch>
Знаете ли вы статьи и т.д где показано, как на самом деле нужно писать React c TypeScript'ом ?
Изменение свойств (props) компонента, полученных от родителя или перерисовка родителя.
class Parent extends React.Component {
state = {
someStateKey: false,
};
someHandler = () => {
this.setState({ someStateKey: true });
};
render() {
return (
<Wrapper>
<ChildA someHandler={this.someHandler} />
<ChildB stateKey={this.state.someStateKey} />
</Wrapper>
);
}
}