【问题标题】:How to use history.push(/nextpath) or windows.location?如何使用 history.push(/nextpath) 或 windows.location?
【发布时间】:2020-01-13 00:03:58
【问题描述】:

我在 stories.jsx 文件中有一个 div(图像),我需要给它 onClick 函数。我如何制作handeClick 函数,让我转到下一页story.jsx(故事API)?当点击其中一个时,此 API 将为它拥有的每个 div(图像)显示不同的故事。

我的图像位于公共(静态)文件夹中,我使用namestoryTextimagePath 将它们保存在 MongoDB 中。但是我需要一个带有history.push(/nextpath) 的句柄函数,才能让我进入故事页面的第一个页面,具体取决于我单击的图像。要么是那个函数,要么是当我按下图像时给我 id 的函数,然后用window.location 将我带到图像。

我也不知道,我是新手。我将如何通过实际示例准确使用history.push(/nextpath) 或其他东西?这是我的this.state,下面是我的 stories.jsx 文件中的render(){}。我该怎么办?

constructor(props) {
    super(props);
    this.state = {
      items: []
    };

这会将所有图像获取到story.jsx:

router.get("/slike", (req, res) => {
  Slike.find((err, result) => {
    console.log(err);
    console.log(result);
    res.send(result);
  });
});

这会从 stories.jsx 中获取一张图片:

router.get("/slike/:id", (req, res) => {
  Slike.findById(req.params.id, (err, result) => {
    res.send(result);
  });
});

【问题讨论】:

  • 你在使用 react-router-dom?
  • 我会,但不是在这两个文件上,我用它来路由应用程序的其余部分,但即使这样我也不知道如何在这里使用
  • 反应我的意思是在你的服务器上表达如果你想路径参数来路由检查这个:reacttraining.com/react-router/web/example/url-params

标签: node.js reactjs express react-router


【解决方案1】:

你可以用它来做到这一点。

import createHistory from 'history/createBrowserHistory'
const history = createHistory();
history.push('/path_to_go')

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 1970-01-01
    • 2020-07-07
    • 2018-01-21
    • 2021-02-01
    • 2020-06-16
    • 2021-07-10
    • 2016-05-18
    • 2018-06-08
    相关资源
    最近更新 更多