GithubHelp home page GithubHelp logo

Comments (1)

joykare avatar joykare commented on August 15, 2024

Conversation that happened around this:
Farhan: Hey team so on my ticket the second part indicates that I create a constructor for each component that uses state however, one of the key uses of property initializers is to not use the constructor so as to have readable components. the other advantage is that We can use arrow functions for custom component methods (and avoid having to bind this). https://www.fullstackreact.com/articles/use-property-initializers-for-cleaner-react-components/

John Paul: We need the constructor to allow us to initialize the constructor of the base class (React.Component) so that we can use this.* in the constructor. This would come in handy when you need to store the props in the state.

Farhan: First of all we should not store props in the state reason being that when ever the props of the component changes with-out the component being refreshed….the new prop will never be displayed cause the constructor function will never update the current state of the component.

Joy: Using the class constructor does not in anyway prevent you from using arrow functions.
Why the constructor is important is that we could initialise many other things like what John said
E.g I can store props in state and therefore be able to do some stuff on the props if they need refactoring in that component https://reactjs.org/docs/react-component.html#constructor
Just an example:

constructor() {
    super();
    this.state = {
      printed: props.printed
    };
    componentWillReceiveProps(nextProps) {
          this.setState({
            printed: nextProps.printed
         })
   }
  }

Using this.state={} is readable but for class-based components it ties the number of things you can do. It might work at first but in the long run leads to unwanted consequences check react blog post I just sent.

from andela-societies-frontend.

Related Issues (20)

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.