Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active September 20, 2016 14:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/fa596c3a0f481088e087e72998d894cd to your computer and use it in GitHub Desktop.
Save ryanflorence/fa596c3a0f481088e087e72998d894cd to your computer and use it in GitHub Desktop.
import { Match, Redirect } from 'react-router'
// With the way we use Redirect in this demo, the word "AddressBar" probably
// makes more sense!
const AddressBar = ({ url }) => (
<Redirect to={url}/>
)
class InfniteArticleHomePage extends React.Component {
state = {
articles: [ this.props.initialArticle ]
}
getNextArticle = () => {
fetchArticle((article) => {
this.setState({
articles: this.state.articles.concat([ article ])
})
})
}
render() {
const { articles } = this.state
return (
<ScrollBottomNotifier onScrollBottom={this.getNextArticle}>
<div>
{articles.map((article) => (
// all articles will match, so they will all render at any
// article's URL
<Match pattern="/article/:id" component={Article}/>
))}
{articles.length > 1 && (
// set the address bar to the last article we've loaded
// if you're still bothered that it's a redirect, consider
// this an input ... that's not weird anymore.
<AddressBar url={`/article/${articles[articles.length - 1].id}}/>
)}
</div>
</ScrollBottomNotifier>
)
}
}
@ferdinandsalis
Copy link

ferdinandsalis commented Sep 20, 2016

Using Redirect wouldnt that mean each call to it would replace the last url — which would mean you cant go back in history. How would I approach it if want to maintain the previous urls. On a side note, I love what you guys did with react-router.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment