[英]Here is code where I have to display information through various buttons. The issue is that when I click one button, the other buttons display info
I used axios to retrieve the data and I tried to add toggle switches, but I wasn't able to figure it out.我使用 axios 来检索数据并尝试添加拨动开关,但我无法弄清楚。 Prettier 2.7.1 Playground link
Prettier 2.7.1 Playground 链接
--parser babel
Input:输入:
function ButtonClick(){
const [users, setUsers] = useState([])
const fetchData = () => {
axios.get("https://iznfqs92n3.execute-api.us-west-1.amazonaws.com/dev/api/v2/movies")
.then(response => {
setUsers(response.data)
})
}
return (
<div>
<div>
<h2>Bond Database</h2>
<Button onClick={() => fetchData()}>Bond Actors</Button>
{users.length > 0 && (
<ul>
{users.map(user => (
<li key={user.id}>{user.bond_actor}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Directors</Button>
{users.length > 0 && (
<ul>
{users.map(user => (
<li key={user.id}>{user.director}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Songs</Button>
{users.length > 0 && (
<ul>
{users.map(user => (
<li key={user.id}>{user.title_song}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Movie Years</Button>
{users.length > 0 && (
<ul>
{users.map(user => (
<li key={user.id}>{user.movie_year}</li>
))}
</ul>
)}
</div>
</div>
)
}
export default ButtonClick
Output: Output:
import React, { useState } from "react";
import styled from "styled-components";
import axios from "axios";
function ButtonClick() {
const Button = styled.button`
background-color: black;
color: white;
font-size: 20px;
padding: 10px 60px;
border-radius: 5px;
margin: 10px 0px;
cursor: pointer;
`;
const [users, setUsers] = useState([]);
const fetchData = () => {
axios
.get(
"https://iznfqs92n3.execute-api.us-west-1.amazonaws.com/dev/api/v2/movies"
)
.then((response) => {
setUsers(response.data);
});
};
const Actors = () => {};
return (
<div>
<div>
<h2>Bond Database</h2>
<Button onClick={() => fetchData()}>Bond Actors</Button>
{users.length > 0 && (
<ul>
{users.map((user) => (
<li key={user.id}>{user.bond_actor}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Directors</Button>
{users.length > 0 && (
<ul>
{users.map((user) => (
<li key={user.id}>{user.director}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Songs</Button>
{users.length > 0 && (
<ul>
{users.map((user) => (
<li key={user.id}>{user.title_song}</li>
))}
</ul>
)}
</div>
<div>
<Button onClick={() => fetchData()}>Movie Years</Button>
{users.length > 0 && (
<ul>
{users.map((user) => (
<li key={user.id}>{user.movie_year}</li>
))}
</ul>
)}
</div>
</div>
);
}
export default ButtonClick;
I believe that I may have to index somehow a way to make sure the button I press is the only button being pressed and not the only one.我相信我可能必须以某种方式编制索引,以确保我按下的按钮是唯一被按下的按钮,而不是唯一一个。 I just don't know how to implement it with my code.
我只是不知道如何用我的代码来实现它。
You are using the same state for all the buttons, which is why you get the data in all places.您对所有按钮使用相同的 state,这就是您在所有位置获取数据的原因。
I just added a type to differentiate which button I clicked;我只是添加了一个类型来区分我点击了哪个按钮; based on that;
基于此; the data is displayed on UI.
数据显示在 UI 上。
Below is the simple logic that I applied.下面是我应用的简单逻辑。
const fetchData = (type) => {
axios
.get(
"https://iznfqs92n3.execute-api.us-west-1.amazonaws.com/dev/api/v2/movies"
)
.then((response) => {
setUsers({ [type]: response.data });
});
};
I'm assigning data based on the state based on the type.我根据类型分配基于 state 的数据。 The state data can be rendered like
users.band.map
. state 数据可以像
users.band.map
一样呈现。 I am attaching the sandbox for reference.我附上沙箱以供参考。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.