简体   繁体   English

这是我必须通过各种按钮显示信息的代码。 问题是当我单击一个按钮时,其他按钮会显示信息

[英]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.我附上沙箱以供参考。

编辑 muddy-cdn-q08wiz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React 中创建了多过滤按钮,但每次单击其他过滤按钮时列表都不会显示 - Created multi-Filtered buttons in React but the list won't display everytime I click on other filter button 我在一个组件中同时使用两个按钮,当我单击它而不是另一个按钮时,我只希望一个按钮起作用 - i am using two buttons simultaneously in one component, i want only one button to work when i click it not the other button 当我单击一个按钮时,它同时打开所有按钮 - When i click one button its open all buttons simultaneously 我想在输入字段中只接受 1-24 并禁用所有其他按钮。 这怎么可能 - I want to accept only 1-24 in the input field and disable all other buttons. how is it possible 如何在 React 中实现多个按钮,当您单击一个按钮时,所有其他按钮都被取消选择 - How to implement multiple buttons in React and when you click one button, all the other buttons are deselected 我想单击框上的任何位置以将我导航到除按钮之外的帖子详细信息页面。 它的按钮应该是它自己的工作 - I want to click anywhere on the box to navigate me to the post detail page except the buttons. It's buttons should it's own job 我有3个CSS按钮,但另一个有<br /> - I have 3 CSS buttons but the other one has <br /> 我有多个 onClick 按钮,每当我单击 1 个按钮时,所有相同的按钮都会触发。 Reactjs - I have multiple onClick button and whenever I click 1 button all the same buttons triggers. Reactjs 我有一个 Button.tsx 文件,我在其中使用情感样式来设置 3 个按钮的样式 - I have a Button.tsx file where I am using emotion styled to style 3 buttons 我必须单击两次显示按钮才能将数据呈现到屏幕上? - I have to click the display button twice to render the data to the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM