【问题标题】:In React 16 and Bootstrap 4, how do I map each tab in bootstrap Tabs component to a URL?在 React 16 和 Bootstrap 4 中,如何将引导选项卡组件中的每个选项卡映射到 URL?
【发布时间】:2020-07-18 16:33:04
【问题描述】:

我正在使用 Bootstrap 4 构建一个 React 16.13.0 应用程序。我想在特定组件 src/components/Edit.jsx 上使用 Tabs ...

import React, { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { Tabs, Tab } from "react-bootstrap";

import FormContainer from "../containers/FormContainer";
import ListPeople from "../components/people/ListPeople";
import { DEFAULT_COUNTRY_CODE } from "../utils/constants";

const { REACT_APP_PROXY } = process.env;

const Edit = (props) => {
  const { id } = useParams();
  const [key, setKey] = useState("home");
  const [coop, setCoop] = useState(null);

  useEffect(() => {
    if (coop == null) {
      fetch(REACT_APP_PROXY + "/coops/" + id)
        .then((response) => {
          return response.json();
        })
        .then((data) => {
          const coop = data;
          coop.addresses.map((address) => {
            address.country = { code: address.locality.state.country.code };
          });
          console.log("edit cop ...");
          console.log(coop);
          setCoop(data);
        });
    }
  }, [props]);

  if (coop == null) {
    return <></>;
  }
  return (
    <div className="container">
      <h1>{coop.name}</h1>

      <Tabs id="controlled-tabs" activeKey={key} onSelect={(k) => setKey(k)}>
        <Tab eventKey="home" title="Home">
          <FormContainer coop={coop} />
        </Tab>
        <Tab eventKey="people" title="People">
          <ListPeople coop={coop} />
        </Tab>
      </Tabs>
    </div>
  );
};

export default Edit;

我还没有弄清楚如何将每个选项卡映射到 URL?现在我有“家”和“人”标签。我想将“主页”选项卡映射到“/edit/

【问题讨论】:

    标签: reactjs bootstrap-4 bootstrap-tabs react-tabs


    【解决方案1】:

    使用react-router 非常简单:

    import {Route} from 'react-router-dom';
    
    const Edit = () => {
      const { id } = useParams();
      ...
      return (
        <Route path="/edit/:id/:tab">
          {({ match, history }) => {
            const { tab } = match ? match.params : {};
    
            return (
              <Tabs
                activeKey={tab}
                onSelect={(nextTab) => history.replace(`/edit/${id}/${nextTab}`)}
              >
                ...
              </Tabs>
            );
          }}
        </Route>
      );
    };
    

    这是example

    或者,如果您的父路由路径看起来像/edit/:id/:tab,那么您可以:

    const Edit = () => {
      const { id, tab } = useParams();
      const history = useHistory();
      ...
      return (
        <Tabs activeKey={tab} onSelect={(nextTab) => history.replace(`/edit/${id}/${nextTab}`)}>
        // or if you wish, you can use history.push instead of history.replace
          ...
        </Tabs>
      );
    };
    

    【讨论】:

    • 嗨,您的意思是使用“history.replace(/edit/${id}/${nextTab})”而不是“history.replace(/tab/${id}/${nextTab})”吗?我得到带有标签版本的空白页。 “history.replace”是否也在搞乱我的历史?当我在选项卡之间单击,然后单击后退按钮时,浏览器似乎不记得我以前通过单击选项卡访问的任何 URL。
    • 当然,在你的情况下应该是history.replace(`/edit/${id}/${nextTab}`)。这只是一个错字。您可以改用history.pushhistory.replace 只是一个例子。
    猜你喜欢
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多