我已經(jīng)創(chuàng)建了一個(gè) React 應(yīng)用程序,我在后端使用 .Net Core,成功接收了來自后端的數(shù)據(jù)列表,但是在使用 Map 時(shí),它只顯示列表中的一項(xiàng)。我使用 MObX 進(jìn)行狀態(tài)管理。我的代碼是:import React, { useContext, useEffect } from 'react'import { RootStoreContext } from '../../app/stores/rootStore';import { observer } from 'mobx-react-lite';import { Segment, Item, Icon, Button } from 'semantic-ui-react';import { format } from 'date-fns';import { Link } from 'react-router-dom';const BookList: React.FC = () => { const rootStore = useContext(RootStoreContext); const { loadBooks, getAvailableBooks } = rootStore.bookStore; useEffect(() => { loadBooks(); }, [loadBooks]); return ( <div> {getAvailableBooks.map(books => ( <Segment.Group key={books.bookName}> <Segment> <Item.Group> <Item> <Item.Image size='tiny' circular src='/assets/user.png' /> <Item.Content> <Item.Header as='a'>{books.bookName}</Item.Header> </Item.Content> </Item> </Item.Group> </Segment></Segment.Group> ))}</div> )}export default observer(BookList);我的書店是:import { observable, action, computed, runInAction } from "mobx";import agent from "../api/agent";import { RootStore } from "./rootStore";import { IBooks } from "../models/books";export default class BookStore { rootStore: RootStore; constructor(rootStore: RootStore) { this.rootStore = rootStore; } @observable bookRegistry = new Map(); @observable book: IBooks | null = null; @observable loadingInitial = false; @computed get getAvailableBooks() { return Array.from(this.bookRegistry.values()); }
地圖方法僅顯示列表中的一項(xiàng)
繁星淼淼
2022-12-02 15:49:47