ئخاشئئشی
New Coder
Hello, where do you think the problem is with the piece of code when it is written in react, the answer is different.
import React, {Fragment} from 'react';
import './App.css';
const allowlist = ["Jon", "Jane"];
const allowlistOnly = (target: any, memberName: string) => {
let currentValue: any = target[memberName];
Object.defineProperty(target, memberName, {
set: (newValue: any) => {
if (!allowlist.includes(newValue)) {
return;
}
currentValue = newValue;
},
get: () => currentValue
});
};
//*********************************
class Person {
@allowlistOnly
name: string = "Jon";
}
function App() {
const person = new Person();
console.log(person.name);
person.name = "Peter";
console.log(person.name);
person.name = "Jane";
console.log(person.name);
return <Fragment/>;
}
export default App;
//*************************
It will be answered
Jon
Peter
Jane
If it should be
Jon
Jon
Jane
enter code here
enter code here
import React, {Fragment} from 'react';
import './App.css';
const allowlist = ["Jon", "Jane"];
const allowlistOnly = (target: any, memberName: string) => {
let currentValue: any = target[memberName];
Object.defineProperty(target, memberName, {
set: (newValue: any) => {
if (!allowlist.includes(newValue)) {
return;
}
currentValue = newValue;
},
get: () => currentValue
});
};
//*********************************
class Person {
@allowlistOnly
name: string = "Jon";
}
function App() {
const person = new Person();
console.log(person.name);
person.name = "Peter";
console.log(person.name);
person.name = "Jane";
console.log(person.name);
return <Fragment/>;
}
export default App;
//*************************
It will be answered
Jon
Peter
Jane
If it should be
Jon
Jon
Jane
enter code here
enter code here