Listing 4-7: An improved version of the Farm component
Old McDonald had a farm.
On his farm he had some pigs.
On his farm he had some cows.
On his farm he had some chickens.
import {useState} from 'react';
import ImprovedFarm from './ImprovedFarm';
export default function ImprovedFarms(){
const initialFarms = [{
farmer:"Old McDonald",
animals:['pigs','cows','chickens']}];
const [farms] = useState(initialFarms);
return(
<>
{
farms?
farms.map((farm,index)=><ImprovedFarm key={index} farmer={farm.farmer} animals={farm.animals} />):
null
}
</>
)
}
Download the examples, report issues, and ask/answer questions in the discussion area by visiting the book's github page. All of the code for the book is also available on codesandbox.io for you to play around with.
ReactJS Foundations is published by John Wiley and Sons, Inc and is available in paperback and eBook.