how to access a property inside an another property in the same class

c# copy properties from one class to another
copy properties from one object to another c#
copy properties from one object to another java
c# mapping one class to another
copy properties from one object to another javascript
copy values from one object to another c# using linq
c# convert one object to another with same properties
c# deep copy using reflection

I'm a react noob player, I was just hanging around my code to add some more options to it, now I want to do something that needs accessing a property in the state object from another property inside the same state object, this is my code:

class App extends Component {
    state = {
        persons : [
          {name: "parsa", age: 45},
          {name: "cena", age: 98},
          {name: "jamil", age: 23}
        ],
    defaultPersons: [...this.state.persons]
    }
}

Here i need to copy the persons property in the defaultPersons property, is it possible ? how can i do that ?

sorry about any confusion, and

Thanks Everyone :)

You can define persons outside of state, and spread the array into the needed places. I will however say that you should really think about why your are doing this. Is there a way you can dry out your code so you don't need two identical objects in state?

constructor(props) {
    super(props);
    const persons = [{key:'value'}, {key:'value'}];
    this.state = {
       persons: [...persons],
       defaultPersons: [...persons],
    }
}

Property Copying Between Two Objects using Reflection, Sometimes you have an object which has a lot of public properties, but you only need a handful 1 2 3 4 5 6 7 8 9 10 11 12 13 public class User { public string object for another task and copy Name and Lastname properties from the User object. We will use the same code as above to fill the methods:. A property inside a class can be declared as abstract by using the keyword abstract. Remember that an abstract property in a class carries no code at all. The get/set accessors are simply represented with a semicolon. In the derived class we must implement both set and get assessors.

There is simple way to achieve what do you need: initialize state in constructor

class App extends Component {
    constructor() {
         this.state = {
             defaultPersons : [
               {name: "parsa", age: 45},
               {name: "cena", age: 98},
               {name: "jamil", age: 23}
             ]
         };
         this.state.persons = [...this.state.defaultPersons];
     }
}

But there are some nuances.

  1. State should contain only dynamic information that affects how component looks(since any change in .state with setState trigger render()). Do you really need defaultPersons to be part of .state? I believe it should be better to set up it directly into this.defaultPersons or even const outside this.

  2. Operation you asked for(and my variant above) makes shallow copy. So any mutation of defaultPersons[1] may change persons[1] item if reference has not been changed yet. And although mutating data in state is bad idea by itself I wanted to highlight this thing.

Object methods, "this", A function that is the property of an object is called its method. these objects do the same user = { sayHi : function ( ) { alert ( "Hello" ) ; } } For instance, the code inside user. To access the object, a method can use the this keyword. If we decide to copy user to another variable, e.g. admin = user and  The C# property model allows external classes to inspect (or set) a given member as though it were a public 'field', and the implementation details are left to the property's accessor and mutator. In your case, you want to expose TotalCost and hide the implementation details about how it is derived. And your code reflects best practices.

This is not possible in one step because there's no this.state.persons to refer until it was defined, this is chicken-egg situation. This is not specific to class properties, the same problem would appear in other situations:

const foo = {
  bar: 1,
  baz: foo.bar // there's no foo at this moment.
};

The problem results from the fact that defaultPersons shouldn't be a part of the state. It seems to be a constant that is not changed. In this case defaultPersons can be a separate property:

class App extends Component {
    static defaultPersons = [
      {name: "parsa", age: 45},
      {name: "cena", age: 98},
      {name: "jamil", age: 23}
    ];

    state = {
        persons : [...App.defaultPersons]
    }

    revertToDefaultPersons() {
        this.setState(state => ({
            ...state,
            persons : [...App.defaultPersons]
        }));
    }
}

Or just a constant that is defined outside the class. This makes things simpler but may result in lesser testability.

Setting and Getting Property Values - Manual, Data object properties can be accessed using the object property access company name to 'Acme', with the same result as Access via property name In addition, when inside a class method function, indirect access to object properties also works: Copyright © 2001-2020 The PHP Group · My PHP.net · Contact · Other  Access to an object field or property from another class c#. The totalAmount() method is an example with Java of how I use to access an value of an object in another class.

you could also use componentDidMount to update the original state

class App extends Component {
    state = {
        persons : [
          {name: "parsa", age: 45},
          {name: "cena", age: 98},
          {name: "jamil", age: 23}
        ],
    defaultPersons: []
    }

     componentDidMount () {
     this.setState({
     defaultPersons: [...this.state.persons]
    })
  }
}

Properties - Manual, Within class methods non-static properties may be accessed by using object (​usually the object to which the method belongs, but possibly another object, giving "Fatal error: Cannot access empty property in test_class.php on line 8" In php, properties are the same as attributes, simple variables without functionality. Similarly another method which is Method2() is being defined with 'public' access specifier and 'void' as return type and inside that Method2() the Method1() is called. Hence, this program shows that a method can be called within another method as both of them belong to the same class.

static, The static keyword defines a static method for a class. In order to call a static method within another static method of the same class, you can use the this keyword. STATIC_METHOD_NAME() or by calling the method as a property of Error: Permission denied to access property "x" · InternalError: too  The ability to dynamically access class properties can significantly decrease the development time required for a project. For example, say we need to map one object’s properties into another

Accessing Class Components, When accessing class components, there is a difference between access from When a component is accessed from inside, that is within a method of the same class, In this context, it is important to note that the properties of instance methods Self-reference allows an object to pass a reference to itself to other objects. The modifier can be used on var properties declared inside the body of a class (not in the primary constructor, and only when the property does not have a custom getter or setter) and, since Kotlin 1.2, for top-level properties and local variables. The type of the property or variable must be non-null, and it must not be a primitive type.

Dynamic Properties - Adding Properties to an Instance, You can add instance data to an object derived from the dynamicprops class. It is possible for more than one program to define dynamic properties on the same object. Access dynamic property values from object arrays, with restricted syntax. Access the dynamic property just like any other property, but only on the  We can access private variable of a class in a different class in so many ways. Here is some of them: By using Public Method. We can access a private variable in a different class by putting that variable with in a Public method and calling that method from another class by creating object of that class.

Comments
  • What have you tried doing so far?
  • state = is already intended for initial state. What's the purpose of defaultPersons? The code can possibly be improved depending on how it's used.
  • I'd use the library prop-types and put in default props to pass to App.
  • Man, you are AWESOME, that is what I was looking for, Thanks :)
  • Glad it helped.