Reputation: 43
i am rendering animal named component in the app component and in the animal component i am maping the animal array from the state and sending the data to card component.card componrnt are making cards for every animals.basically like this App->home>card. now that i have a remove butoon in the card compo i need the bring the card animal name into the app compo once the button is clicked. how i am able to do so ??
how can i bring the animal.name inside the this.removeHandeler so that i can remove the card using the remove button in card component.
App component
import React, { Component } from "react";
import { BrowserRouter, Route, Routes } from "react-router-dom";
import { animals, birds } from "./component/Data";
import Navbar from "./component/Navbar";
import Home from "./component/Home";
import Animal from "./component/Animal";
import Bird from "./component/Bird";
import About from "./component/About";
import "./App.css";
export default class App extends Component {
state = { animals: animals, birds: birds, search: "" };
removeHandeler = (name) => {
console.log(name);
const UpdatedArray = this.state.animals.filter(
(animal) => animal.name !== name
);
this.setState({ animals: UpdatedArray });
};
render() {
return (
<BrowserRouter>
<Navbar />
<Routes>
<Route path="/" element={<Home />} />
<Route
path="/animal"
element={
<Animal
animals={this.state.animals}
removeCard={() => this.removeHandeler(animal.name)}
/>
}
/>
<Route path="/bird" element={<Bird birds={this.state.birds} />} />
<Route path="/about" element={<About />} />
</Routes>
</BrowserRouter>
);
}
}
Animal component
import React from "react";
import Card from "./Card";
const animal = ({ animals, removeCard }) => {
const animalList = animals.map((animal) => {
return (
<Card
name={animal.name}
likes={animal.likes}
key={animal.name}
removeCard={removeCard}
/>
);
});
return (
<div className="container">
<div>
<h1> Animal List</h1>
<input />
</div>
<div className="animalList_container "> {animalList} </div>
</div>
);
};
export default animal;
card component
import React from "react";
const Card = (props) => {
return (
<div className="card">
<div className="image_wrapper">
<button className="removeButton" onClick={props.removeCard}>
X
</button>
<img
src={`https://source.unsplash.com/500x400/?${props.name} `}
alt={`${props.name} `}
/>{" "}
</div>
<div className="animal_info">
<h2>{props.name[0].toUpperCase() + props.name.slice(1)}</h2>
<div className="social_wrapper">
<button onClick={props.removeLikes}> Remove </button>
<p>{props.likes} ♥</p>
<button onClick={props.addLikes}> Add like </button>
</div>
</div>
</div>
);
};
export default Card;
Upvotes: 2
Views: 40
Reputation: 113
Your Card
component has the name of the animal, so you can just call your removeHandeler
function from there. Simply pass it in the Animal
props like this :
<Animal
animals={this.state.animals}
removeCard={this.removeHandeler.bind(this)}
/>
And in the Card
component, call it in the onClick
:
<button className="removeButton" onClick={() => {props.removeCard(props.name)}}>
Upvotes: 2