【问题标题】:why isn't my router redirecting - interactive map?为什么我的路由器没有重定向 - 交互式地图?
【发布时间】:2020-11-02 01:47:18
【问题描述】:

我有这张交互式地图,如果您单击某个国家/地区,它会将您重定向到该特定国家/地区的页面,但重定向对我不起作用,我不明白我做错了什么。这是代码:

onClick={() => {
                    const {NAME} = geo.properties;
                    if(NAME == 'Bulgaria')
                      {
                        <Redirect to='pages/services' />
                      }
                  }}

这是我的交互式地图的 .js 文件的完整代码:

import React, { memo } from "react";
import {
  ComposableMap,
  Geographies,
  Geography
} from "react-simple-maps";
import { Redirect } from "react-router-dom";
import { BrowserRouter } from 'react-router-dom';

...
const MapChart = ({ setTooltipContent }) => {
  return (
    <>
      <ComposableMap data-tip="" projectionConfig={{ scale: 200 }}>

          <Geographies geography={geoUrl}>
            {({ geographies }) =>
              geographies.map(geo => (
                <Geography
                  key={geo.rsmKey}
                  geography={geo}
                  onMouseEnter={() => {
                    const { NAME, POP_EST } = geo.properties;
                    setTooltipContent(`${NAME} — ${rounded(POP_EST)}`);
                  }}
                  onMouseLeave={() => {
                    setTooltipContent("");
                  }}
                  onClick={() => {
                    const { NAME } = geo.properties;
                    if (NAME === 'Bulgaria') {
                    this.setState({ redirect: true });
                    }
                  }}
                  ...
                />
              ))
            }
          </Geographies>
      </ComposableMap>
    </>
  );
};

export default memo(MapChart);

【问题讨论】:

    标签: javascript reactjs redirect router


    【解决方案1】:

    问题

    这不是 JSX 的工作方式。它没有重定向,因为该函数无法返回可渲染的 JSX,也不会尝试导航。呈现的所有内容都需要在 render 函数中,或函数组件返回。此处理程序不返回任何内容。

    解决方案

    您可以以声明的方式有条件地渲染 Redirect 组件,也可以强制发出导航操作。

    在条件下使用Redirect 组件。

    // later in render/return
    if (redirect) {
      return <Redirect to='/pages/services' />;
    }
    
    return (
      ...
    
      onClick={() => {
        const { NAME } = geo.properties;
        if (NAME === 'Bulgaria') {
          setRedirect(true);
        }
      }}
    
      ...
    );
    

    地图

    const MapChart = ({ setTooltipContent }) => {
      const [redirect, setRedirect] = useState(false); // <-- add redirect state
    
      return redirect ? ( // <-- conditional render redirect
        <Redirect to="/pages/services" />
      ) : (
        <ComposableMap data-tip="" projectionConfig={{ scale: 200 }}>
          <Geographies geography={geoUrl}>
            {({ geographies }) =>
              geographies.map((geo) => (
                <Geography
                  ...
                  onClick={() => {
                    const { NAME } = geo.properties;
                    if (NAME === "Bulgaria") {
                      setRedirect(true); // <-- set redirect state
                    }
                  }}
                  ...
                />
              ))
            }
          </Geographies>
        </ComposableMap>
      );
    };
    

    使用history.replace

    onClick={() => {
      const { NAME } = geo.properties;
      if (NAME === 'Bulgaria') {
        history.replace('/pages/services');
      }
    }};
    

    地图

    const MapChart = ({ history, setTooltipContent }) => { // <-- destructure history route prop
      return (
        <ComposableMap data-tip="" projectionConfig={{ scale: 200 }}>
          <Geographies geography={geoUrl}>
            {({ geographies }) =>
              geographies.map((geo) => (
                <Geography
                  ...
                  onClick={() => {
                    const { NAME } = geo.properties;
                    if (NAME === "Bulgaria") {
                      history.replace("/pages/services"); // <-- imperative navigation
                    }
                  }}
                  ...
                />
              ))
            }
          </Geographies>
        </ComposableMap>
      );
    };
    

    注意:如果MapChart 不是由Route 组件直接呈现(即它没有收到route props),那么您可以使用useHistory反应钩子。

    const history = useHistory();
    

    点击处理程序的导航将完全相同。

    【讨论】:

    • 对于我提出的非常愚蠢的问题,我提前道歉,因为这是我第一次使用 JavaScript。在哪里添加渲染功能?坦率地说,我有点失落。我将添加我的所有代码作为我的问题的答案
    • @Emiliya 不用担心。您的 sn-p 中没有足够的上下文来知道您是在渲染基于类的还是功能性的反应组件。基于类的组件具有render 生命周期函数,而函数组件的整个函数体被认为是“渲染”函数。我已更新我的答案以反映您的功能组件。
    • 太棒了,好心的先生。
    猜你喜欢
    • 2019-06-02
    • 1970-01-01
    • 2016-07-12
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多