import React, { FC, useEffect } from 'react';
import { RouteComponentProps, withRouter } from 'react-router-dom';
interface OwnProps {
error: string;
}
type Props = OwnProps & RouteComponentProps<any>;
const HandlerErr: FC<Props> = ({ error, history }) => {
useEffect(() => {
const timeout = setTimeout(() => {
history.push(`/`);
}, 2000);
return () => {
clearTimeout(timeout);
};
}, [error]);
return (
<>
<div>{error}</div>
<div>Contact site administrator</div>
</>
);
};
export default withRouter(HandlerErr);
import React, { useState, useRef, useCallback, useMemo } from 'react';
import debounce from 'lodash/debounce';
import { fetchSomeData } from './someplace';
const Example = () => {
const [results, setResults] = useState([]);
const inputEl = useRef(null);
const handleTintClick = useCallback((value) => {
inputEl.current.value = value;
}, [inputEl]);
const handleInputChange = useMemo(() => debounce(e => {
const { value } = e.target;
if (value.length < 3) return;
fetchSomeData(value).then(setResults);
}, 800), []);
return (
<>
<input ref={inputEl} onChange={handleInputChange} />
{results.length > 0 && (
<ul>
{results.map((result, i) => (
<li
onClick={() => hanldeTintClick(result.title}}
key={result.id}
>
{result.title}
</li>
)}
</ul>
)}
</>
);
};
undefined || undefined && 'до этого места проверка не доходит'
undefined || 'что угодно' // 'что угодно'
undefined && 'что угодно' // undefined
npm run eject
на тестовом проекте и посмотреть в файлах config/webpack.config.js
, config/webpackDevServer.config.js
и scripts/start.js
как это все настроено и работает.npm run build
и откройте директорию build/static
, то увидите там все сгенерированные файлы. toJSON() {
return '[Helper Name]';
}
const PrivateRoute = ({ component: Component, render, isSignedIn, ...rest }) => (
<Route
{...rest}
render={props => {
if (!isSignedIn) return (
<Redirect
to={{
pathname: '/login',
state: { referrer: props.history.location.pathname },
}}
/>
);
if (render) return render({ ...props });
return <Component {...props} />;
}}
/>
);
const mapStateToProps = (state) => ({
isSignedIn: isSignedInSelector(state),
});
export default connect(mapStateToProps)(PrivateRoute);
import React, { useState, useCallback } from 'react';
import WeatherPage from './WeatherPage';
import Flag from './Flag';
const FlagsList = ({ onItemClick, flagsList }) => (
<ul>
{flagsList.map(flag => <Flag key={flag.id} flag={flag} onClick={onItemClick} />)}
</ul>
);
const Example = () => {
const [shouldShowWeather, setShouldShowWeather] = useState(false);
const toggleShowWeather = useCallback(() => {
setShouldShowWeather(state => !state);
}, []);
return (
<>
{shouldShowWeather && <WeatherPage />}
<FlagsList flagsList={flagsList} onItemClick={toggleShowWeather} />
</>
);
};
this.setState(state => ({
data: state.data.filter(el => el.id !== id),
}));