Destructuring array to call a function in it as in the useState function
Wed Jun 21 2023 12:45:16 GMT+0000 (Coordinated Universal Time)
Saved by
@sarfraz_sheth
#react.js
const animals = [
{ name: "cat", sound: "meow" },
{ name: "dog", sound: "woof" }
];
function useAnimal(animal) {
return [animal.name, function() {
console.log(animal.sound);
}
];
}
export default animals;
export { useAnimal };
------- App Page ------------
mport React from "react";
import ReactDOM from "react-dom";
import animals, { useAnimal } from "./data";
const [catName, dogName] = animals;
const { name, sound } = catName;
console.log(useAnimal(catName));
const [animal, makeSound] = useAnimal(catName);
console.log(makeSound());
console.log(makeSound());
makeSound();
content_copyCOPY
haw to use destructuring by applying function in array and call te function by a name using the destructuring method
Comments