Advertisement
Guest User

Untitled

a guest
Sep 24th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. # React Code Guidelines
  2.  
  3. ## Component Composition
  4.  
  5. When declaring a React component, be sure to make it as discrete as possible to maximize its reusability potential. This includes not having another class method to render DOM elements. It should be its own discrete component so that other parts of the application can potentially reuse this component, instead of having it pegged to a top level component as a class method. All components, even top level class components, should never have `render` functions more than 25-35 lines long. If it needs to be longer than that, then you need to recheck your code and see how you can break things down further into more discrete components. Consider the following **anti-pattern**:
  6.  
  7. ```jsx
  8. import React, { Component } from 'react'
  9.  
  10. class Todo extends Component {
  11. constructor(props) {
  12. super(props)
  13. }
  14. renderTodoItem = () => {
  15. return (
  16. <li>I am a Todo item</li>
  17. )
  18. }
  19. render() {
  20. <div>
  21. <p>Hello World!</p>
  22. <ul>
  23. { this.renderTodoItem }
  24. </ul>
  25. </div>
  26. }
  27. }
  28.  
  29. export default Todo
  30. ```
  31.  
  32. Instead, what should be done is the following:
  33.  
  34. ```jsx
  35. import React from 'react'
  36.  
  37. const TodoItem = (props) => {
  38. return (
  39. <li>
  40. { props.text }
  41. </li>
  42. )
  43. }
  44.  
  45. export default TodoItem
  46. ```
  47.  
  48. You then `import` this component to the top level class component to render. This ensures maximum reusability and sane structural organization of components. Which brings us to the next point, most of your components should be discrete _stateless functional components_. You should avoid handling state internally within a component as much as possible. Why? Because it ensures a sane downward state flow pattern which is easy to both build tests for and to understand the overall movement of data throughout your application.
  49.  
  50. On top of that, by rendering components through class methods, you strip away all of the special optimizations catered to stateless functional components. There are performance optimizations specific to stateless functional components that avoid unnecessary checks and memory allocations.
  51.  
  52. ## Stateless Functional Components
  53.  
  54. With the advent of ES6 syntax, it is now possible to declare components as _stateless functional components_. Consider the following:
  55.  
  56. ```jsx
  57. import React from 'react'
  58.  
  59. const SignUpForm = (props) => {
  60. return (
  61. <form onSubmit={props.onSubmit}>
  62. <input type='text' name='username' onChange={props.onChange} />
  63. <input type='email' name='email' onChange={props.onChange} />
  64. <input type='password' name='password' onChange={props.onChange} />
  65. <button>Sign Up</button>
  66. </form>
  67. )
  68. }
  69.  
  70. export default SignUpForm
  71.  
  72. ```
  73.  
  74. Here, we have declared a stateless functional component that handles all the form DOM elements. It doesn't have an internal state at all, which keeps things concise and far easier to reason about when we import it to the following top level class component:
  75.  
  76. ```jsx
  77. import React, { Component } from 'react'
  78. import { Link } from 'react-router'
  79.  
  80. import SignUpForm from '../components/SignUpForm'
  81.  
  82. class SignUp extends Component {
  83. constructor(props) {
  84. super(props)
  85.  
  86. this.state = {
  87. username: '',
  88. email: '',
  89. password: ''
  90. }
  91. }
  92. handleInputChange = (e) => {
  93. this.setState({
  94. [e.target.name]: e.target.value
  95. })
  96. }
  97. handleSubmit = (e) => {
  98. e.preventDefault()
  99.  
  100. // dispatch state data to API endpoint here
  101. }
  102. render() {
  103. return (
  104. <div>
  105. <SignUpForm onSubmit={this.handleSubmit}
  106. onChange={this.handleInputChange} />
  107. <span>
  108. Already have an account?
  109. <Link to='/login'>Log In</Link>
  110. </span>
  111. </div>
  112. )
  113. }
  114. }
  115.  
  116. export default SignUp
  117. ```
  118.  
  119. By setting a `name` to the DOM element, it allows us to abstract the `<input>` handler into a single class method, rather than making a new class method for each `<input>` element. This is just an example of how you should strive to abstract and modularize your code in the most concise way possible.
  120.  
  121. ## Component Styling
  122.  
  123. Never inline component styles at all. **Ever**. They should be kept in their own respective CSS/LESS/SASS files.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement