Skip to content

Instantly share code, notes, and snippets.

@rivertam
Created May 16, 2016 16:37
Show Gist options
  • Save rivertam/8ea5ded762e12fa01aa0f84ceb243c4f to your computer and use it in GitHub Desktop.
Save rivertam/8ea5ded762e12fa01aa0f84ceb243c4f to your computer and use it in GitHub Desktop.
Rendering the OpenedArticle
class OpenArticle extends Component {
static propTypes = {
article: PropTypes.article.isRequired,
getCard: PropTypes.func.isRequired,
openArticle: PropTypes.oneOfType([PropTypes.bool, PropTypes.article]).isRequired,
};
shouldComponentUpdate(newProps) {
const oldOpenSlug = this.props.openArticle && this.props.openArticle.slug;
const newOpenSlug = newProps.openArticle && newProps.openArticle.slug;
const openNow = oldOpenSlug === this.props.article.slug;
const shouldOpen = newOpenSlug === this.props.article.slug;
if (openNow !== shouldOpen) {
if (openNow) {
console.log(`%c[closing] %c${this.props.article.slug}`, 'color: red;', '');
} else {
console.log(`%c[opening] %c${this.props.article.slug}`, 'color: green;', '');
}
}
return openNow !== shouldOpen;
}
render() {
const { article, openArticle } = this.props;
// console.log(`%c[rendering] %c${article.id}: ${article.slug}`, 'color: blue', '');
if (!openArticle || openArticle.slug !== article.slug) {
return <TransitionGroup />;
}
return (
<TransitionGroup>
<OpenedArticle key={article.slug} {...this.props}>
{article.headline}
</OpenedArticle>
</TransitionGroup>
);
}
}
@rivertam
Copy link
Author

This seems to be working properly; closing and opening when necessary.

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