с чем это связано
let s: string;
s = 182;
const [prod, setProd] = useState<FakeProductList | null>(null);
if (!prod) {
}
return (/* ... */);
componentDidMount() {
setTimeout(() => {
updateStore(1); // <--- WTF?
});
}
import React, { useRef, useCallback } from 'react';
import ReactDOM from 'react-dom';
import { Provider, connect } from 'react-redux';
import { createStore } from 'redux';
const UPDATE = 'UPDATE';
const update = (payload) => ({
type: UPDATE,
payload,
});
const reducer = (state = 0, action) => {
if (action.type === UPDATE) {
return action.payload;
}
return state;
}
const store = createStore(reducer);
const mapStateToProps = state => ({ state });
const mapDispatchToProps = { update };
const Header = ({ state }) => (
<div>Header. State: {state}</div>
);
const ConnectedHeader = connect(mapStateToProps)(Header);
const Body = ({ state, update }) => {
const inputEl = useRef(null);
const handler = useCallback(() => {
update(inputEl.current.value);
}, [inputEl, update]);
return (
<div>
<div>Body. State: {state}</div>
<input type="number" ref={inputEl} />
<button onClick={handler}>Update state</button>
</div>
);
}
const ConnectedBody = connect(
mapStateToProps,
mapDispatchToProps,
)(Body);
const App = () => (
<Provider store={store}>
<ConnectedHeader />
<ConnectedBody />
</Provider>
);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
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>
)}
</>
);
};
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),
}));
interface Filter {
name?: string;
title?: string;
}
interface FiltersProps {
filters: Filter[];
}
const Filters: React.FC<FiltersProps> = ({ filters }) => (
<div>
{filters.map((filter: Filter) => (
<div key={filter.name}>
{filter.title}
{filter.name}
</div>
))}
</div>
);
const Main: React.FC = () => (
<Filters filters={filters} />
);
interface State {
shouldShowBtn: boolean;
}
class MyComponent extends React.Component<null, State> {
state = {
shouldShowBtn: true,
};
componentDidMount() {
window.addEventListener('scroll', this.scrollHandler);
}
scrollHandler = () => {
const { shouldShowBtn } = this.state;
if (window.pageYOffset > 50 && shouldShowBtn) {
this.setState({ shouldShowBtn: false });
} else if (window.pageYOffset <= 50 && !shouldShowBtn) {
this.setState({ shouldShowBtn: true });
}
};
componentWillUnmount() {
window.removeEventListener('scroll', this.scrollHandler);
}
render() {
return (
<>
{this.state.shouldShowBtn && <button>lioih </button>}
</>
);
}
}
const MyComponent: React.FC = () => {
const [shouldShowBtn, setShouldShowBtn] = useState(true);
const context = useMemo(() => ({ shouldShowBtn }), []);
useEffect(() => {
context.shouldShowBtn = shouldShowBtn;
}, [shouldShowBtn]);
useEffect(() => {
const scrollHandler = () => {
if (window.pageYOffset > 50 && context.shouldShowBtn) {
setShouldShowBtn(false);
} else if (window.pageYOffset <= 50 && !context.shouldShowBtn) {
setShouldShowBtn(true);
}
};
window.addEventListener('scroll', scrollHandler);
return () => {
window.removeEventListener('scroll', scrollHandler);
};
}, []);
return (
<>
{shouldShowBtn && <button>lioih </button>}
</>
);
};
const useGetState = (initialState: any) => {
const [state, _setState] = useState(initialState);
const context = useMemo(() => ({ state }), []);
const getState: any = useCallback(() => context.state, []);
const setState = useCallback((state) => {
context.state = state;
_setState(state);
}, []);
return [getState, setState];
};
const MyComponent: React.FC = () => {
const [getShouldShowBtn, setShouldShowBtn] = useGetState(true);
useEffect(() => {
const scrollHandler = () => {
console.log(window.pageYOffset, getShouldShowBtn());
if (window.pageYOffset > 50 && getShouldShowBtn()) {
setShouldShowBtn(false);
} else if (window.pageYOffset <= 50 && !getShouldShowBtn()) {
setShouldShowBtn(true);
}
}
window.addEventListener("scroll", scrollHandler);
return () => {
window.removeEventListener("scroll", scrollHandler);
};
}, []);
return <div>{getShouldShowBtn() && <button>lioih </button>}</div>;
};