gpt4 book ai didi

reactjs - 无状态组件 React 路由器

转载 作者:行者123 更新时间:2023-12-03 13:32:58 24 4
gpt4 key购买 nike

我是 React 和 Redux 的新手,实际上我正在创建自己的代码,但我被无状态组件内的路由器之类的东西卡住了。

所以,实际上我需要使用 this.props.history.push('/somepath') 路由到组件。这不会发生在无状态组件内。

我的无状态组件是

import React from "react"; // eslint-disable-line no-unused-vars
import "bootstrap/dist/css/bootstrap.min.css";
import "./header.css";

const Header = () => ({

handleAuthor() {
this.props.history('/somepath')// this is not working
},

render(){
return (
<div className = "header">
<h1 onClick = {this.handleAuthor.bind(this)}>{this.props.headerText}</h1>
</div>
);
}
});

export default Header;

我在另一个无状态组件(如 mainlayout)中调用它

import React from "react"; // eslint-disable-line no-unused-vars
import Header from "../header/Header"; // eslint-disable-line no-unused-vars
import Footer from "../footer/Footer"; // eslint-disable-line no-unused-vars
import "./mainLayout.css";

const MainLayout = props => ({
render() {
return (
<div className="App">
<Header headerText = "RK Boilerplate"/>
<div className = "mainLayout">
<main>{props.children}</main>
</div>
<Footer />
</div>
);
}
});

export default MainLayout;

我的主文件index.js看起来像这样

import React from "react"; // eslint-disable-line no-unused-vars
import ReactDOM from "react-dom"; // eslint-disable-line no-unused-vars
import { matchRoutes, renderRoutes } from "react-router-config"; // eslint-disable-line no-unused-vars
import { Router } from "react-router-dom"; // eslint-disable-line no-unused-vars
import { Switch } from "react-router"; // eslint-disable-line no-unused-vars
import { Provider } from "react-redux"; // eslint-disable-line no-unused-vars
import store from "./store"; // eslint-disable-line no-unused-vars
import routes from "./routes"; // eslint-disable-line no-unused-vars
import MainLayout from "./components/mainLayout/MainLayout"; // eslint-disable-line no-unused-vars

import createHistory from "history/createBrowserHistory";
let history = createHistory();
const App = document.getElementById("app");

export default App;

ReactDOM.render(
<Provider store={store}>
<MainLayout>
<Router history= {history}>
<Switch>
{renderRoutes(routes)}
</Switch>
</Router>
</MainLayout>
</Provider>,
App);

所以我需要的是我必须从 header 路由到另一个组件,其中该组件和路径在路由器文件中给出

路由器.js

import Home from "../containers/Home";
import About from "../containers/About";
import CreateUser from "../containers/CreateUser";

import Layout from "../containers/Layout";

const routes = [
{ path: "/",
exact: true,
component: Layout
},
{ path: "/home",
exact: true,
component: Home
},
{
path:"/About",
exact: true,
component: About
},
{
path:"/createUser",
exact: true,
component: CreateUser
}
];

export default routes;

当我尝试从 header 路由时,出现类似 Push of undefined 的错误。我错过了什么吗?我应该在这里做任何改变吗?

提前致谢。

最佳答案

2019 年,React Router v4 现已添加用于无状态/功能组件的 useHistory 钩子(Hook):

https://reacttraining.com/react-router/web/api/Hooks/usehistory

来自文档:

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

function HomeButton() {
let history = useHistory();

function handleClick() {
history.push("/home");
}

return (
<button type="button" onClick={handleClick}>
Go home
</button>
);
}

关于reactjs - 无状态组件 React 路由器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50895607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com