const CTX = React.createContext();
const App = () => (
<CTX.Provider value={ctx}>
<MyComponent />
</CTX.Provider>
);
const CTX = React.createContext(ctx);
const App = () => (
<MyComponent />
);
useEffect(() => {
doSomething();
}, [match.params.id]);
<Redirect
to={{
pathname: '/login',
state: { referrer: props.history.location.pathname }, // или `/products/${slug}`
}}
/>
class Login extends React.Component {
onLoginSuccess() {
const { history } = this.props;
const { state } = history.location;
const location = (state && state.referrer) || '/';
history.push(location);
}
/* ... *.
}
Поскольку Header не участвует в роутинке я не могу от него передать в Search props.history и уже на нем вызвать пуш а не как сейчас на прямую!
export default withRouter(Header);
const Foo = ({ data }) => {
const dispatch = useDispatch();
useEffect(() => {
dispatch(func(data));
}, [data]);
return (/* ... */);
}
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);
arr.reduce((a, c, i) => {
if (c == arr[i - 1]) {
a[a.length - 1] += c;
} else {
a.push(c);
}
return a;
}, []);
с чем это связано
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);