【问题标题】:React react router dom [duplicate]反应反应路由器dom [重复]
【发布时间】:2021-11-18 10:46:32
【问题描述】:

import { withRouter } from 'react-router-dom'


const MenuItem =({title,imageUrl,size,history,linkUrl,match})=>(
    <div 
     className={`${size} menu-item`} onClick={()=> history.push(`${match.url}${linkUrl}`)}>
        <div className='background-image'
            style={{
        backgroundImage: `url(${imageUrl})`
    }}
        />
            <div className="content">
                <h1 className="title">
                    {title.toUpperCase()}
                </h1>
                <span className="subtitle">
                    Shop Now
                </span>
            </div>
        </div>
)
export default withRouter(MenuItem)

尝试导入错误:“withRouter”未从“react-router-dom”导出。我应该如何在 v6 中替换它

【问题讨论】:

标签: javascript reactjs react-router-dom


【解决方案1】:

withRouter 在 react-router v6 中是 deprecated。你应该改用钩子。

在这种情况下,使用useNavigate,如下所示:

import { useNavigate } from "react-router-dom";

const MenuItem = ({ title, imageUrl, size, history, linkUrl, match }) => {
  const navigate = useNavigate();

  return (
    <div
      className={`${size} menu-item`}
      onClick={() => navigate(`${match.url}${linkUrl}`)}
    >
      <div
        className="background-image"
        style={{
          backgroundImage: `url(${imageUrl})`,
        }}
      />
      <div className="content">
        <h1 className="title">{title.toUpperCase()}</h1>
        <span className="subtitle">Shop Now</span>
      </div>
    </div>
  );
};

【讨论】:

  • onClick={() => navigate(${match.url}${linkUrl})} 可以吗???它给了我一个错误,我现在删除了 matchurl,它的 okk tnx 寻求帮助
猜你喜欢
  • 2020-12-21
  • 2018-03-14
  • 1970-01-01
  • 2022-01-21
  • 2017-12-30
  • 1970-01-01
  • 2020-06-21
  • 2018-02-16
相关资源
最近更新 更多