meta-level method in React js:
As an example,we are creating a library that treat object differently,if we want to implement some special function.The below code shows that defining a property key for such a method and implementing it look like this:
1import "./styles.css";
2import React from "react";
3const specialSymbol = Symbol("specialSymbol");
4export default class App extends React.Component {
5 constructor(props) {
6 super(props);
7 this.state = {
8 obj: {
9 id: 1,
10 [specialSymbol]() {
11 console.log("iji");
12 return this.id;
13 }
14 }
15 };
16 }
17 handleCheck = (abc) => {
18 let obj = {
19 id: 1,
20 [abc]() {
21 console.log("iji");
22 return this.id;
23 }
24 };
25 const a = this.state.obj[specialSymbol]();
26 console.log("a", obj);
27 };
28 render() {
29 return (
30 <>
31 <button onClick={() => this.handleCheck("test1")}>Click 1 </button>
32 <button onClick={() => this.handleCheck("test2")}>Click 2 </button>
33 <button onClick={() => this.handleCheck("test3")}>Click 3 </button>
34 </>
35 );
36 }
37}
About
Moiz is a software engineer from Arid University with a passion for writing tech tutorials and doing coding. I am continously produce JavaScript and other web development technology tutorials in concepts through easy-to-understand explanations written in plain English.I have expertise in next js ,react js ,javascript,html,bootstrap and node js.
Follow him on TwitterHassan is a software engineer from kohat university of science and technology with a passion for writing tech tutorials and doing javascript practices on daily basis.I have expertise in next js ,react js ,javascript,html,bootstrap and node js. Problem Solving and making concpets is my strong point.
Follow him on TwitterTags
Click to see all tutorials tagged with: