Coder Social home page Coder Social logo

syjin2 / react-clean-code Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thawkin3/react-clean-code

0.0 0.0 0.0 387 KB

Simple examples to write cleaner React code

Home Page: http://tylerhawkins.info/react-clean-code/build/

JavaScript 69.63% CSS 3.71% HTML 26.66%

react-clean-code's Introduction

React Clean Code

Simple examples to write cleaner React code

Examples

  1. Conditional rendering only for one condition: If you need to conditionally render something when a condition is true and not render anything when a condition is false, don't use a ternary, use the && operator.

  2. Conditional rendering on either condition: If you need to conditionally render one thing when a condition is true and render a different thing when a condition is false, use a ternary.

  3. Boolean props: A truthy prop can be provided to a component with just the prop name like myTruthyProp. Writing it like myTruthyProp={true} is unnecessary.

  4. String props: A string value can be provided as a prop value in double quotes without the use of curly braces or backticks.

  5. Unnecessary anonymous event handler functions: If an event handler only takes a single argument for the Event object, you can just provide the function as the event handler like onChange={handleChange}. You don't need to wrap the function in an anonymous function like onChange={e => handleChange(e)}.

  6. Unnecessary anonymous function components: When passing a component as a prop to another component, you don't need to wrap this passed component in a function if the component does not accept any props.

  7. Undefined props: Undefined props are excluded, so don't worry about providing an undefined fallback if it's ok for the prop to be undefined.

  8. Setting state that relies on the previous state: Always set state as a function of the previous state if the new state relies on the previous state.

Honorable Mention

The following practices are not React-specific but rather are good practices for writing clean code in JavaScript (and any programming language, for that matter).

  • Extract complex logic into clearly-named functions
  • Extract magic numbers into constants
  • Use clearly named variables

react-clean-code's People

Contributors

thawkin3 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.