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();
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter