Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Forked from smoidu/test.js
Last active April 5, 2016 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmsnell/0c053fb25a69f5c7e54f11a155c3af63 to your computer and use it in GitHub Desktop.
Save dmsnell/0c053fb25a69f5c7e54f11a155c3af63 to your computer and use it in GitHub Desktop.
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