|
1 |
| -import React from 'react'; |
2 |
| -import { connect } from 'react-redux'; |
3 |
| -import browserHistory from '../browserHistory'; |
| 1 | +import { useEffect } from 'react'; |
| 2 | +import { useSelector } from 'react-redux'; |
| 3 | +import { useHistory } from 'react-router-dom'; |
| 4 | +import PropTypes from 'prop-types'; |
4 | 5 |
|
5 |
| -const RedirectToUser = ({ username, url = '/:username/sketches' }) => { |
6 |
| - React.useEffect(() => { |
7 |
| - if (username == null) { |
8 |
| - return; |
| 6 | +const RedirectToUser = ({ url = '/:username/sketches' }) => { |
| 7 | + const history = useHistory(); |
| 8 | + const username = useSelector((state) => |
| 9 | + state.user ? state.user.username : null |
| 10 | + ); |
| 11 | + useEffect(() => { |
| 12 | + if (username) { |
| 13 | + history.replace(url.replace(':username', username)); |
9 | 14 | }
|
10 |
| - |
11 |
| - browserHistory.replace(url.replace(':username', username)); |
12 |
| - }, [username]); |
| 15 | + }, [history, url, username]); |
13 | 16 |
|
14 | 17 | return null;
|
15 | 18 | };
|
16 | 19 |
|
17 |
| -function mapStateToProps(state) { |
18 |
| - return { |
19 |
| - username: state.user ? state.user.username : null |
20 |
| - }; |
21 |
| -} |
22 |
| - |
23 |
| -const ConnectedRedirectToUser = connect(mapStateToProps)(RedirectToUser); |
24 |
| - |
25 |
| -const createRedirectWithUsername = (url) => (props) => ( |
26 |
| - <ConnectedRedirectToUser {...props} url={url} /> |
27 |
| -); |
| 20 | +RedirectToUser.propTypes = { |
| 21 | + url: PropTypes.string.isRequired |
| 22 | +}; |
28 | 23 |
|
29 |
| -export default createRedirectWithUsername; |
| 24 | +export default RedirectToUser; |
0 commit comments