Giter Site home page Giter Site logo

props-and-lists's Introduction

Props, lists and events ๐Ÿพ

In this repo you will practise everything covered today.

The project was done with create-react-app, so you just need to run:

npm install
npm run start

Iteration 1: Print the cards ๐Ÿงพ

On App.jsx:

  • Import the hook useState and create an "animals" state, with the "setAnimals" updater function. Its initial value should be the animalData array that's already imported from the JSON on App.jsx.
  • Use the .map() method to print one card for each animal of the array. Check the Card component to see how the component expects the props.
  • โš ๏ธ Remember that each element inside a map should have a unique key property โš ๏ธ
Click here ONLY if you need to see the solution
// App.jsx

import './App.css';
import React, { useState } from 'react';
import animalData from './data.json';
import Card from './components/Card';

function App() {
  const [animals, setAnimals] = useState(animalData);

  return (
    <div className="App">
      <h1>Adopt me plz ๐Ÿพ</h1>
      {animals.map(elem => {
        return <Card key={elem._id} animal={elem}/>
      })}
    </div>
  );
}

export default App;

Iteration 2: Conditional rendering ๐ŸŽฏ

You will see that some animals don't have an image, and this is the visual result:

We can fix it by using conditional rendering on Card.jsx:

Click here ONLY if you need to see the solution
// components/Card.jsx

import React from 'react'

export default function Card(props) {
  const { animal: { name, lifeExpectancy, description, needsMedicine, image } } = props;
  
  return (
    <div className="card">
      <h3>{name}</h3>
      {image ? <img src={image} alt={name} width="100%"/> : 
      <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/7/70/Dog_silhouette.svg/2067px-Dog_silhouette.svg.png" alt={name} width="100%" />}
      <p>{description}</p>
      <p>This animal will live {lifeExpectancy} years</p>
      {needsMedicine && <p className="red-color">This animal needs medicine</p>}
    </div>
  )
}

Sort by life expectancy

On App.jsx:

  • Create a function called handleSortByLife
  • This function should sort the animals array and update the state with a new array, sorted by life expectancy from less years to more years
  • Add a button to your app that, when clicked, whill trigger the "handleSortByLife". When creating the button, remember that you have a class for it called action-btn.
Click here ONLY if you need to see the solution
// App.jsx

function App() {
  const [animals, setAnimals] = useState(animalData);

  const handleSortByLife = () => {
    const ordered = [...animals].sort((a, b) => a.lifeExpectancy - b.lifeExpectancy)
    setAnimals(ordered);
  }

  return (
    <div className="App">
      <h1>Adopt me plz ๐Ÿพ</h1>
      <button onClick={handleSortByLife} className="action-btn">Sort by life expectancy</button>
      {animals.map(elem => {
        return <Card key={elem._id} animal={elem}/>
      })}
    </div>
  );
}

Filter dogs

On App.jsx:

Now we want another button that, when clicked, will filter the animals and return only the dogs. Try to do that without further instructions and, when you are finished, check at the solution.

You can do it!!! ๐Ÿ’ช๐Ÿผ

Click here to see the solution
// App.jsx

import './App.css';
import React, { useState } from 'react';
import animalData from './data.json';
import Card from './components/Card';

function App() {
  const [animals, setAnimals] = useState(animalData);

  const handleSortByLife = () => {
    const ordered = [...animals].sort((a, b) => a.lifeExpectancy - b.lifeExpectancy)
    setAnimals(ordered);
  }

  const handleFilterDogs = () => {
    const filtered = animals.filter(elem => elem.type === "dog");
    setAnimals(filtered);
  }

  return (
    <div className="App">
      <h1>Adopt me plz ๐Ÿพ</h1>
      <button onClick={handleSortByLife} className="action-btn">Sort by life expectancy</button>
      <button onClick={handleFilterDogs} className="action-btn">See only dogs</button>
      {animals.map(elem => {
        return <Card key={elem._id} animal={elem}/>
      })}
    </div>
  );
}

export default App;

All done โœ…

props-and-lists's People

Contributors

alebausa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.