Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an new instance of an Object in React?

Obviously in normal JS I can do this

var Card = function(rank, suit){
    this.rank = rank; 
    this.suit = suit
  }

var cardOne = new Card('3', 'H');

cardOne // Card {rank: "3", suit: "H"}

So how would I do that in react and ES6 land?

I have tried something like this:

class ReactApp extends React.Component{

  Card = (rank, suit) => {
    this.rank = rank;
    this.suit = suit;
  };

  createCard = () => {
    let CardObj = {};
    let card = new this.Card('3', 'Hearts');
    console.log(card);
  };

}

(not showing render method for now)

but how can I get that to log the correct thing in react? how are functions treated inside React? (key value pairs?) and how do i define objects etc?

like image 391
The worm Avatar asked Dec 09 '16 16:12

The worm


People also ask

How do you create a new instance of an object in React?

The simple answer is to declare the Card class separately from your component, e. g. class Card { constructor(rank, suit) { this. rank = rank; this. suit = suit; } } class ReactApp extends React.

What is an instance in React?

An instance is what you refer to as this in the component class you write. It is useful for storing local state and reacting to the lifecycle events. Function components don't have instances at all. Class components have instances, but you never need to create a component instance directly—React takes care of this.

What is an instance of a component?

In UML modeling, component instances are model elements that represent actual entities in a system. You typically use component instances in deployment diagrams to represent implementation units that exist at run time; however, you can also use them in component diagrams.

How do you access an object inside an object in React?

If we want to access all the values of nested objects then we have to use recursion to access each and every level of that object. And it can get more complicated according to the nesting of the object. That why we have to use recursion to get all the values and access the whole nested object.


2 Answers

If you are looking for Card model you can create a new ES6 class for that

export class Card {
  constructor(rank, suit) {
    this.rank = rank;
    this.suit = suit;
  }
}

After this, you can import that model into react component as

import {Card} from './card'
like image 192
Jagadish Upadhyay Avatar answered Sep 28 '22 03:09

Jagadish Upadhyay


A little bit late, but still...

Since React v16.8 when hooks were introduced it's been suggested to use functional components over class ones.

const Card = function(rank, suit) {
    const rank = rank;
    const suit = suit;
    return { rank, suit };
};

const cardOne = Card("3", "H");

cardOne; // {rank: "3", suit: "H"}
cardOne.rank; // "3"
cardOne.suit; // "H"

But that is a little old-styled. Most elegant way to do this in one line of code using arrow function:

const Card = (rank, suit) => { return { rank: rank, suit: suit } }

That's all. Now you can assign your variables.

const cardOne = Card('3', 'H')

cardOne // {rank: "3", suit: "H"}
cardOne.rank // "3"
cardOne.suit // "H"

You can also add export in front of your constant to make it importable from anywhere:

// components.js
export const Card = (rank, suit) => { return { rank: rank, suit: suit } }


// App.js
import { Card } from './components'

const cardTwo = Card('2', 'F')
cardTwo // {rank: "2", suit: "F"}
cardTwo.rank // "2"
cardTwo.suit // "F"

Also you should better use const and let for declaring variables instead of var because of hoisting. Here is a good article explaining why.

like image 31
Your Ad Here Avatar answered Sep 28 '22 01:09

Your Ad Here