gpt4 book ai didi

javascript - React Router Dom 不渲染任何组件

转载 作者:太空宇宙 更新时间:2023-11-04 15:34:31 25 4
gpt4 key购买 nike

我有一个非常简单的项目,React router dom 不是渲染组件。有时它会渲染第一个路由的组件,但随后不会渲染。

这是 App.js 的代码:

import React, { Component } from 'react';
import { Provider } from "react-redux";
import {
BrowserRouter as Router,
Route
} from "react-router-dom";

import store from "./store";
import createHistory from 'history/createBrowserHistory';

import { IntroJm } from './components/intro/jm';
import { IntroNfc } from './components/intro/nfc';
import { IntroTxn } from './components/intro/txn';

import './assets/sass/main.scss';
import './static/css/main.scss';
import './static/css/pure.min.scss';
import './App.scss';

let browserHistory = createHistory();

class App extends Component {

componentDidMount() {
browserHistory.push("/intro/jm");
}

render() {
return (
<Provider store={store}>
<div>
<Router>
<div>
<Route path="/intro/jm" component={IntroJm} />
<Route path="/intro/nfc" component={IntroNfc} />
<Route path="/intro/txn" component={IntroTxn} />
</div>
</Router>
</div>
</Provider>
);
}
}

export default App;

所有路由组件都是仅带有span标签的简单组件。

请帮忙

最佳答案

您可以尝试使用 WithRouter 包装组件,然后使用 this.props.history.push() 更改路由

尝试

import React, { Component } from 'react';
import { Provider } from "react-redux";
import {
BrowserRouter as Router,
Route
} from "react-router-dom";
import {withRouter} 'react-router';

import store from "./store";

import { IntroJm } from './components/intro/jm';
import { IntroNfc } from './components/intro/nfc';
import { IntroTxn } from './components/intro/txn';

import './assets/sass/main.scss';
import './static/css/main.scss';
import './static/css/pure.min.scss';
import './App.scss';

let browserHistory = createHistory();

class App extends Component {

componentDidMount() {
this.props.history.push("/intro/jm");
}

render() {
return (
<Provider store={store}>
<div>
<Router>
<div>
<Route path="/intro/jm" component={IntroJm} />
<Route path="/intro/nfc" component={IntroNfc} />
<Route path="/intro/txn" component={IntroTxn} />
</div>
</Router>
</div>
</Provider>
);
}
}

export default withRouter(App);

关于javascript - React Router Dom 不渲染任何组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44506765/

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