Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import React from 'react';
  2.  
  3.  
  4. export default class ShowOnRoute extends React.Component {
  5. static contextTypes = {
  6. router: React.PropTypes.func.isRequired
  7. };
  8.  
  9. static propTypes = {
  10. name: React.PropTypes.string.isRequired,
  11. wrapper: React.PropTypes.oneOfType([React.PropTypes.element, React.PropTypes.string]),
  12. params: React.PropTypes.object,
  13. query: React.PropTypes.object
  14. };
  15.  
  16. static defaultProps = {
  17. wrapper: 'div',
  18. params: undefined,
  19. query: undefined
  20. };
  21.  
  22. render() {
  23. if (this.context.router.isActive(this.props.name, this.props.params, this.props.query)) {
  24. if (React.Children.count(this.props.children) > 1) {
  25. //Just to prevent dumb-dumbs, we wrap if there are more than one child. Ideally you should just pass in
  26. //a singular child and declare it manually.
  27. return (
  28. <this.props.wrapper {...this.props}>
  29. {this.props.children}
  30. </this.props.wrapper>
  31. );
  32. } else {
  33. return this.props.children;
  34. }
  35. } else {
  36. return null;
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement