Giter Site home page Giter Site logo

doc-js-ways-to-define-components's Introduction

Defining components with React

  • https://leanpub.com/doc-js
  • If you want to contribute to the book or join me as a coauthor pool, please get in contact mgalli at mgalli dot com subject "doc-js book"

From a higher level, smart vs dumb components

According to Vivek Nayya Vivek 2017 developers should be familiar with React's main kinds of comopnents: smart and dumb ones.

  • The dumb kind are the simple ones, with no logic and whose purpose is to provide presentation transformation only.

  • The smart kind may involve logic.

The differentiation relates to how they deal with states (and this may relate to dependency to other logic from other parts of a system). According to Vivek, if a component holds state, then it's smart.

However, in the React way of doing things, you don't have to decide between smart and dumb; in fact most of your code will eventually grow with smart and dumb counterparts alongside. What you do is to keep their afairs in order as the smart ones can deal with state and calls the simplified ones for rendering:

The following example is referred as a container:

import React, { Component } from 'react';
import Product from './Product';

export default class ProductsContainer extends Component {
  constructor(props) {
    super(props);
    this.state = {
      products: [
        'Red Saree',
        'Blue Saree',
        'Green Saree'
      ]
    }
  }
  renderProducts() {
    return this.state.products.map((product) => {
      return <Product name={product} />;
    });
  }
  render() {
    return (
      <div className='products-container'>
        {this.renderProducts()}
      </div>
    );
  }
}

Notice that the above "renderProducts" function will return a tag, , which can be implemented as a simple/dumb component - also source from Vivek

import React, { Component } from 'react';

export default class Product extends Component {
  render() {
    return (
      <div className='product'>
        {this.props.name}
      </div>
    );
  }
}

Mess about the ways to define components

According to SHubham Khatri [2017 StackOverflow], there is a functional component approach, that can be used when your component only takes in props and renders the result directly:

const Main = () => {
   return(<h1>my title</h1>)
}

However, there is also the ES6 class based approach, that can be used when the React component has more functionality and handles states:

class Main extends React.Component {
   render(){ return(<h1>something</h1>}
}

2017 Stack Overflow

What's next here?

According to Jeff Cousins [Cousins 2016], the decision to go with functional is when you can have a component that is limited to using props, as external data, and it simpy produces output via the render – in a pure function fashion. (depends:pure-functions)

The other kinds of components are the ones that have internal states or other internal functionality.

Cousins 2016

Any more?

According to Cory House [2016 Cory]:

"Enforced Best Practices Stateless functional components are useful for dumb/presentational components. Presentational components focus on the UI rather than behavior, so it’s important to avoid using state in presentational components. "

2016 Cory

Cory also asks us to have the state to be managed at a higher level in the hierarchy, at container level components or via Flux/Redux.

So, avoiding the temptation of adding state and logic to a presentational component

According to Cory, "it’s always tempting to add state to a presentational component when you’re in a hurry".

Moving on towards functional components

According to Cory House, [2016 Cory] there are 9 wins to look at when using functional components:

2016 Cory

Related

doc-js-ways-to-define-components's People

Contributors

taboca avatar

Watchers

 avatar  avatar  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.