diff --git a/src/components/ArticleList.js b/src/components/ArticleList.js index b9bbdfc..2f3a0b8 100644 --- a/src/components/ArticleList.js +++ b/src/components/ArticleList.js @@ -1,17 +1,15 @@ import React, {Component} from 'react' import Article from './Article' +import toggleOpenArticle from '../decorators/toggleOpenArticle' -export default class ArticleList extends Component { - state = { - openArticleId: null - } - +//еще propTypes нужны были +class ArticleList extends Component { render() { const articleElements = this.props.articles.map(article =>
  • ) @@ -22,7 +20,6 @@ export default class ArticleList extends Component { ) } - toggleOpenArticle = openArticleId => ev => { - this.setState({ openArticleId }) - } -} \ No newline at end of file +} + +export default toggleOpenArticle(ArticleList) diff --git a/src/decorators/toggleOpenArticle.js b/src/decorators/toggleOpenArticle.js new file mode 100644 index 0000000..3b116a7 --- /dev/null +++ b/src/decorators/toggleOpenArticle.js @@ -0,0 +1,22 @@ +import React, {Component as ReactComponent} from 'react' + +//WrappedComponent я называл чтоб легче понять было. Лучше выбирай более значущее название +export default (OriginalComponent) => class WrappedComponent extends ReactComponent { + + state = { + //Не привязывайся к названиям сущностей, вся суть декораторов в универсальности. Сделай openItemId + openArticleId: null + } + + toggleOpenArticle = openArticleId => ev => { + if (this.state.openArticleId === openArticleId) + { this.setState({ openArticleId: null }) } + else { + this.setState({ openArticleId }) + } + } + + render () { + return + } +}