React 添加带有复选框的收藏夹

发布于 2025-01-18 06:23:40 字数 2222 浏览 2 评论 0原文

对象 收藏 我正在尝试做一个有反应的网站,我使用 API 来接收数据。我收到的数据被放入一个列表中,然后我为该列表中的每个项目生成一个按钮。现在我还为列表中的每个项目生成一个复选框,但生成是在单独的组件中。我想要做的是,如果选中一个项目的复选框,则该项目应存储在缓存中,并作为单独页面上的按钮再次显示。我现在的问题是我该怎么做?

先感谢您。

这是我生成复选框的地方:

import React from "react";
export default function Favcheck() {
  
  
  return (
    <>
         <div class="favcheck">
           Favorit
           <input type="checkbox" name="name"  class="checkbox" id="heart" />
          </div>
      
    </>
  );
}

这是制作按钮的地方:

import axios from "axios";
import * as React from "react";
import Favcheck from "./favcheck.jsx";
import Mensapage from "./mensapage.jsx";
import site from "./home.jsx";

export default function Mensbuttons(props) {
  
  return (
    <>
      
      <div class="formcontainer">
        <form method="get" action="/mensapage" id="mensaform">
          <button  type="submit" class="mensabutton" key={props.key}>
            <div class="mensatext">{props.name}</div>
          </button>
          <br></br>
          <Favcheck />
        </form>
      </div>
     
    </>
  );
}

这是使用按钮的地方:

import  React,{ useState, useEffect } from "react";
import axios from 'axios';

import Nav from "./nav.jsx";

import Mensbuttons from "./mensbuttons.jsx";


export default function Home(props) {
 
 let site="test";
  
 const[posts,setPosts] = useState([])
  useEffect(()=>{
    axios.get('https://openmensa.org/api/v2/canteens?near[lat]=52.517037&near[lng]=13.38886&near[dist]=15')
    .then(res =>{
     
      setPosts(res.data)
    })
    .catch(err =>{
      console.log(err)
    })
  },[])
   console.log(posts);
  
    
  return (
    <>
    
      <Nav />
      <div class="header">
        <h1>Mensen</h1>
        
      </div>
      {posts.map((list) => {
        return <Mensbuttons name={list.name} key={list.id} />;
      })}
    </>
  );
}

Objects
Favourites
im trying to do a website witch react and i use an api to recieve data. The Data i recieved gets put into a list and then i produce a button for every item in this list. Now i also produce a check box for every item in the list, but the production is in a seperate component. what i want to do ist that, if the checkbox of one item gets checked, the item should be stored in a cache and put out again as an button on a seperate page. My Question now is how do i do that?

Thank you in advance.

This is where i produce the checkbox:

import React from "react";
export default function Favcheck() {
  
  
  return (
    <>
         <div class="favcheck">
           Favorit
           <input type="checkbox" name="name"  class="checkbox" id="heart" />
          </div>
      
    </>
  );
}

this is where the buttons are made:

import axios from "axios";
import * as React from "react";
import Favcheck from "./favcheck.jsx";
import Mensapage from "./mensapage.jsx";
import site from "./home.jsx";

export default function Mensbuttons(props) {
  
  return (
    <>
      
      <div class="formcontainer">
        <form method="get" action="/mensapage" id="mensaform">
          <button  type="submit" class="mensabutton" key={props.key}>
            <div class="mensatext">{props.name}</div>
          </button>
          <br></br>
          <Favcheck />
        </form>
      </div>
     
    </>
  );
}

and this is where the buttons are used:

import  React,{ useState, useEffect } from "react";
import axios from 'axios';

import Nav from "./nav.jsx";

import Mensbuttons from "./mensbuttons.jsx";


export default function Home(props) {
 
 let site="test";
  
 const[posts,setPosts] = useState([])
  useEffect(()=>{
    axios.get('https://openmensa.org/api/v2/canteens?near[lat]=52.517037&near[lng]=13.38886&near[dist]=15')
    .then(res =>{
     
      setPosts(res.data)
    })
    .catch(err =>{
      console.log(err)
    })
  },[])
   console.log(posts);
  
    
  return (
    <>
    
      <Nav />
      <div class="header">
        <h1>Mensen</h1>
        
      </div>
      {posts.map((list) => {
        return <Mensbuttons name={list.name} key={list.id} />;
      })}
    </>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风为裳 2025-01-25 06:23:40

这是一些模型图片
我想通过选中复选框

这是收藏夹
这是带有复选框的按钮

here are some mockup pictures
i want to get specific objects to the favourites page by checking the checkbox

here are the favourites
here are the buttons with checkboxes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文