React State Property referring to properties in own scope

react setstate
react props vs state
react hooks
props react
react functional components
react, ( usestate)
react pass props to component
state management react

The issue here is that this.currentTotal is undefined, which will result in a NaN during this arithmetic: this.currentTotal / 3.

There are a few ways to resolve this, but the perhaps the simplest solution is to just defer the Math.ceil(this.currentTotal / 3) calculation til after your components state is fully initialised like so:

class Component {

  constructor() {
  
    const state = {
      currentTotal: 30,
      columnLength: 0
    }
    
    // Defer calculation til after state has been initialised
    state.columnLength = Math.ceil(state.currentTotal / 3)
    
    // Assign your initialised to the component
    this.state = state

    console.log( this.state.columnLength )
  }
  
}

new Component()

Components and Props – React, You can find a detailed component API reference here. a single “props” (which stands for properties) object argument with data and returns a React element. The Child component reacts according to the property received. In React the "only" proper way to change the state of a component is using the functions componentWillMount or componentDidMount and componentWillReceiveProps as far as I've seen (among others, but let's focus on these ones, because getInitialState is just executed once).


since you already need a currentTotal, I think this solution would be the most elegant

constructor() {
  const currentTotal = 30; // initialization
  this.state = {
    currentTotal,
    columnLength: Math.ceil(currentTotal / 3),
  }
}

Lifting State Up – React, We recommend lifting the shared state up to their closest common ancestor. It will handle the change by modifying its own local state, thus re-rendering both  This function is a valid React component because it accepts a single “props” (which stands for properties) object argument with data and returns a React element. We call such components “function components” because they are literally JavaScript functions.


We do usually to implement switch.

this.setState({ isOpen:!this.state.isOpen });

Mutate state using setState only.

React Quick Tip: Use Class Properties and Arrow Functions to Avoid , Note: You must have transform-class-properties enabled in your own Babel Arrow functions, by their nature, do not re-scope this , so we don't need Notice in the second example, I defined state as a class property as well,  React magic happens when you use state data in the view (for example, to print in if/else as a value of an attribute, or as a child’s property value) and then give setState() new values. Boom! Boom!


React State vs Props explained, Before jumping to state vs props we have to compare a React which is very similar to, you already guessed it a function own local scope. We defined a name property inside Component state and used it inside the render process. State is referred to the local state of the component which cannot be  Working with Data in React: Properties & State. By If a property named class is used, React will display a helpful console message informing the developer that the property name needs to be


Binding functions in React, Components contain their own state and pass down properties to child components. All of these things are referred to using 'this' so if you write a React application, Here is an example with a function in the global scope: 2 Ways to Initialize State. There are two ways to initialize state in a React component: inside the constructor, and directly inside the class. Here are a couple examples. Inside the Constructor. Initializing state inside the constructor looks like this:


Mastering React, The update property needs to be set to the callback function that will be called when the input component's onChange event setState will refer to HelloReact. A big question in React JS is State vs Props. State is essentially any data that relates to the internals of a React component, but has nothing to do with the outside world. Props are data that