Last active
January 30, 2016 06:59
-
-
Save ryanflorence/bbe292d4f5994500bd3b 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
import React from 'react' | |
// need a getter and a hash of requestId:results or something | |
// to use in server rendering | |
let results = [] | |
function updateTitle() { | |
document.title = results[results.length - 1] | |
} | |
const Title = React.createClass({ | |
getInitialState() { | |
return { | |
index: results.push('') - 1 | |
} | |
}, | |
componentWillUnmount() { | |
results.pop() | |
}, | |
componentDidMount: updateTitle, | |
componentDidUpdate: updateTitle, | |
render() { | |
const { render } = this.props | |
results[this.state.index] = typeof render === 'function' | |
? render(results[this.state.index - 1] || '') | |
: render | |
return this.props.children || null | |
} | |
}) | |
export default Title |
Seems strange to me that you would pass children into <Title>
. What is the reasoning for that?
oops, this wont' get updates when parents change
@jehoshua02 I agree, feels stranger to just have a title floating around somewhere in render though, you can do it either way.
Seems a bit gross to changing document.title
within the component. What if you had a updateTitle
function in context? That way, App
can decide what to do with it. Or even a nested component, such as a panel/modal/card, can have its own title hierarchy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can use
<Title/>
anywhere, but nice to see it up top of render so you know it's messing w/ the title.