Guest User

Untitled

a guest
Oct 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. class Content extends React.Component {
  2. constructor(props) {
  3. super(props);
  4. }
  5.  
  6. componentDidMount() {
  7. this.props.dispatch(fetchNav(this.props.match.params.tab));
  8. }
  9.  
  10. componentDidUpdate(prevProps) {
  11. if(this.props.match.params.tab != prevProps.match.params.tab) {
  12. this.props.dispatch(fetchNav(this.props.match.params.tab));
  13. }
  14. }
  15.  
  16. render() {
  17. const {nav, match} = this.props;
  18. let redirect = null;
  19. return (
  20. <div>
  21. <ul>
  22. {nav.some((item, key) => {
  23. if (this.props.location.pathname != (match.url + item.path)) {
  24. redirect = <Redirect to={(match.url + item.path)} />;
  25. }
  26. return true;
  27. })}
  28. {redirect}
  29. {nav.map((item, key) => {
  30. return <li key={key}>
  31. <Link to={match.url + item.path}>{item.name}</Link>
  32. </li>;
  33. })}
  34. <Switch>
  35. {nav.map((item, key) => {
  36. return <Route key={key} path={`${match.url}/list/:tab`} component={Content} />;
  37. })}
  38. </Switch>
  39. </ul>
  40. </div>
  41. );
  42. }
  43. }
  44.  
  45. const mapStateToProps = (state, props) => {
  46. const {fetchNav} = state;
  47. const {
  48. lastUpdated,
  49. isFetching,
  50. nav: nav
  51. } = fetchNav[props.match.params.tab] || {
  52. isFetching: true,
  53. nav: []
  54. };
  55.  
  56. return {
  57. nav,
  58. isFetching,
  59. lastUpdated
  60. }
  61. };
  62.  
  63. export default connect(mapStateToProps)(withStyles(appStyle)(Content));
Add Comment
Please, Sign In to add comment