-
-
Save dmsnell/0c053fb25a69f5c7e54f11a155c3af63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Router, | |
Route, | |
IndexRoute, | |
Redirect, | |
Link, | |
IndexLink | |
} = ReactRouter | |
const Wrapper = React.createClass( { | |
render() { | |
const { children, ...props } = this.props; | |
return <Children { ...props } />; | |
} | |
} ) | |
const Index = React.createClass({ | |
displayName: "Index", | |
componentDidMount() { | |
console.log("Index:DidMount"); | |
}, | |
componentWillUnmount() { | |
console.log("Index:WillUnmount"); | |
}, | |
componentDidUpdate(prevProps) { | |
console.log("Index:DidUpdate"); | |
}, | |
render() { | |
return ( | |
<div> | |
<h1>Index</h1> | |
<Link to="/1">SubComponent</Link> | |
<p /> | |
<Link to="/">Index</Link> | |
</div> | |
) | |
} | |
}) | |
const SubComponent = React.createClass({ | |
displayName: "SubComponent", | |
componentDidMount() { | |
console.log("SubComponent:DidMount"); | |
}, | |
componentWillUnmount() { | |
console.log("SubComponent:WillUnmount"); | |
}, | |
componentDidUpdate(prevProps) { | |
console.log("SubComponent:DidUpdate"); | |
}, | |
render() { | |
return ( | |
<div> | |
<h1>Subcomponent</h1> | |
<Link to="/1">SubComponent</Link> | |
<p /> | |
<Link to="/">Index</Link> | |
</div> | |
) | |
}, | |
}) | |
const App = React.createClass({ | |
displayName: "app", | |
componentDidMount() { | |
console.log("app:DidMount"); | |
}, | |
componentWillUnmount() { | |
console.log("app:WillUnmount") | |
}, | |
componentDidUpdate(prevProps) { | |
console.log("app:DidUpdate"); | |
}, | |
render() { | |
return ( | |
<div> | |
{this.props.children} | |
</div> | |
) | |
} | |
}) | |
const WrapElements = function(Component, props) { | |
if (Component.displayName == "Index" || Component.displayName == "SubComponent" || true) { | |
return <Wrapper><Component { ...this.props }></Wrapper>; | |
} else { | |
return <Component {...props} />; | |
} | |
} | |
React.render(( | |
<Router createElement={WrapElements}> | |
<Route path="/" component={App}> | |
<IndexRoute component={Index} /> | |
<Route path=":id" component={SubComponent} /> | |
</Route> | |
</Router> | |
), document.getElementById('app')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment