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?
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.
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.
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.
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.
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'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With