我正在使用Ionic 7和Reaction 18.我想构建一个下拉组件,以便在 Select 一个选项时,我的URL将更改为包含所选选项的ID.我在我的App.tsx文件中设置了此设置,

<IonHeader>
  <IonToolbar>
    <IonButtons slot="start">
      <IonButton>
        <IonIcon icon={logoReact} />
      </IonButton>
    </IonButtons>
    <IonTitle>My Title</IonTitle>
    <IonButtons slot="end">
      <IonButton>
        <IonIcon icon={search} />
      </IonButton>
      <IonButton>
        <IonIcon icon={personCircle} />
      </IonButton>
    </IonButtons>
  </IonToolbar>
  <IonToolbar>
    <CategorySelectComponent />
  </IonToolbar>
</IonHeader>
...
   <IonReactRouter>
      <IonRouterOutlet>
        <Route exact path="/home">
          <Home />
        </Route>
        <Route exact path="/">
          <Redirect to="/home" />
        </Route>
        <Route exact path="/cards/:categoryId">
          <CardSlideComponent />
        </Route>
      </IonRouterOutlet>

我的 Select 组件是这样的

import {
  IonContent,
  IonItem,
  IonLabel,
  IonPage,
  IonSelect,
  IonSelectOption,
} from "@ionic/react";
import React, { useEffect } from "react";
import { useState } from "react";
import axios from "axios";
import CardService from "../services/CardService";
import { useHistory } from "react-router-dom";

const CategorySelectComponent: React.FC = () => {
  const [selectedValue, setSelectedValue] = useState<string | undefined>(
    undefined
  );
  const [options, setOptions] = useState<Category[]>([]);

  const history = useHistory();
  console.log(history); // this is always undefined

  const handleSelectionChange = (event: CustomEvent) => {
    const selectedCategoryId = event.detail.value;
    setSelectedValue(selectedCategoryId);

    // Navigate to the selected category page
    console.log("handle change ...");
    console.log(history);
    history.push(`/cards/${selectedCategoryId}`);
  };

  useEffect(() => {
    CardService.getCategories(setOptions);
  }, []); // Empty dependency array means this effect runs once on component mount

  return (
    <IonPage>
      <IonContent>
        <IonItem>
          <IonLabel>Choose an option:</IonLabel>
          <IonSelect
            value={selectedValue}
            onIonChange={handleSelectionChange}
            interface="popover"
          >
            {options.map((category) => (
              <IonSelectOption key={category.id} value={category.id}>
                {category.name}
              </IonSelectOption>
            ))}
          </IonSelect>
        </IonItem>

        <IonItem>
          <IonLabel>Selected Value:</IonLabel>
          <IonLabel>{selectedValue}</IonLabel>
        </IonItem>
      </IonContent>
    </IonPage>
  );

但"历史"部分始终是"未定义的". 有没有一种更"ionic "的方式来做这件事,而不涉及历史对象?

推荐答案

我最近遇到了同样的问题,当时我需要使用useIonRouter挂钩来判断是否可以返回导航.您只需要在ReactTree中将路由组件(that provides the routing context, e.g. the 101 object)提升到比使用它的任何组件更高的位置.

IonHeader组件隐藏在IonReactRouter组件中,以便在其路由上下文中呈现CategorySelectComponent.

<IonReactRouter>
  ...

  <IonHeader>
    <IonToolbar>
      <IonButtons slot="start">
        <IonButton>
          <IonIcon icon={logoReact} />
        </IonButton>
      </IonButtons>
      <IonTitle>My Title</IonTitle>
      <IonButtons slot="end">
        <IonButton>
          <IonIcon icon={search} />
        </IonButton>
        <IonButton>
          <IonIcon icon={personCircle} />
        </IonButton>
      </IonButtons>
    </IonToolbar>
    <IonToolbar>
      <CategorySelectComponent />
    </IonToolbar>
  </IonHeader>
  ...
   
  <IonRouterOutlet>
    <Route exact path="/home">
      <Home />
    </Route>
    <Route exact path="/">
      <Redirect to="/home" />
    </Route>
    <Route exact path="/cards/:categoryId">
      <CardSlideComponent />
    </Route>
  </IonRouterOutlet>
</IonReactRouter>

Reactjs相关问答推荐

Firebase删除UserWithEmail AndPassword仅创建匿名用户

CreateBrowserRouter将props 传递给父组件以验证权限

将对象添加到集合中的数组时的no_id字段

Reaction-路由-DOM v6与v5

如何使用TouchStart/TouchEnd在Table ReactJS中交换位置?

React 错误: 只能用作 元素的子元素,从不直接渲染.请将您的 包裹在

错误:无法获取 RSC 负载.返回浏览器导航

无法使用react-hook-form和react-places-autocomplete在字段中输入值

Next.js 和理智 | npm run build 时预渲染页面/时发生错误

使用jest如何覆盖对象的模拟?我正在使用`jest.mock()`来模拟对象,并希望 for each 测试用例覆盖模拟.

ReactJS on AWS Amplify与EC2上的Spring boot服务之间的混合内容错误

React 应用程序可以嵌入到 PowerPoint 中吗?

React 测试库包装器组件

Primereact 的日历组件在每个月 12 日之后使用非唯一键生成

条件渲染元素的测试不起作用

react-router v6 中的 BrowserRouter 和 createBrowserRouter 有什么区别?我可以在不使用数据 API 的情况下使用 createBrowserRouter 吗?

顶点图表甜甜圈项目边框半径

react-markdown 不渲染 Markdown

仅在重新渲染后设置状态

如何在 NavBar 中设置动态标题