From 977690b22632b55ad8d09cdcbf65f60930db3083 Mon Sep 17 00:00:00 2001 From: Jamie Kyle Date: Fri, 23 Aug 2019 15:16:53 -0700 Subject: [PATCH 1/4] update logic & jsx rule, add refactors directory, with logic and jsx refactor --- react/README.md | 184 ++++- refactors/README.md | 65 ++ refactors/logic-and-jsx/0-OriginalFile.js | 336 +++++++++ refactors/logic-and-jsx/1-NoDestructuring.js | 332 +++++++++ .../logic-and-jsx/2-SplitConditionals.js | 381 ++++++++++ refactors/logic-and-jsx/3-InlineLogic.js | 307 ++++++++ .../logic-and-jsx/4-PullIntoComponents.js | 378 ++++++++++ refactors/logic-and-jsx/README.md | 663 ++++++++++++++++++ 8 files changed, 2624 insertions(+), 22 deletions(-) create mode 100644 refactors/README.md create mode 100644 refactors/logic-and-jsx/0-OriginalFile.js create mode 100644 refactors/logic-and-jsx/1-NoDestructuring.js create mode 100644 refactors/logic-and-jsx/2-SplitConditionals.js create mode 100644 refactors/logic-and-jsx/3-InlineLogic.js create mode 100644 refactors/logic-and-jsx/4-PullIntoComponents.js create mode 100644 refactors/logic-and-jsx/README.md diff --git a/react/README.md b/react/README.md index 6d69b5e..00ca0db 100644 --- a/react/README.md +++ b/react/README.md @@ -1621,49 +1621,189 @@ In addition to `render()` being the _last_ method in a component (see [Method or ### Logic and JSX -React and JSX supporting logic and markup in the same file allows for substantial complexity in markup generation over other traditional templating languages (like [handlebars](http://handlebarsjs.com)). But with that increased complexity can come a decrease in readability. +> **Note:** This rule was changed from a previous version that said the +> opposite, read the [refactoring](../refactors/logic-and-jsx) that explains why. -In order to maximize both complexity and readability, we suggest keeping all logic out of JSX, except variable references and method calls. Expressions, particularly ternary expressions, should be stored in variables outside of JSX. +A single component's returned JSX code can get quite complex with lots of +expressions. But instead of pulling those expressions out into variables and +statements, you should focus on pulling out parts of the JSX into seperate +components, and some of the logic into utility functions. ```js -// good -render() { - let {includeHeader} = this.props; - let buttons = [1, 2, 3, 4, 5].map((page) => ( -