Reactjs React路由器链路不存在';不刷新内容,但我';我在用路由器
我将React路由器DomReactjs React路由器链路不存在';不刷新内容,但我';我在用路由器,reactjs,redux,react-router,react-redux,react-router-v4,Reactjs,Redux,React Router,React Redux,React Router V4,我将React路由器Dom4.2.2与Redux一起使用,我无法使正常工作。按下链接时,URL会更改,但内容不会刷新/呈现。我正在使用路由器(连接(…,…)(组件),但仍然不起作用 以下是组件: class IndexPage extends React.Component { constructor(props) { console.log(props.location.state); super(props); this.state
4.2.2
与Redux一起使用,我无法使
正常工作。按下链接时,URL会更改,但内容不会刷新/呈现。我正在使用路由器(连接(…,…)(组件),但仍然不起作用
以下是组件:
class IndexPage extends React.Component {
constructor(props) {
console.log(props.location.state);
super(props);
this.state = {
display: (props.location.state && props.location.state.display) ? props.location.state.display : null
};
}
componentDidMount() {
console.log("[LOADED]IndexPage");
const self = this;
}
render() {
const self = this;
var displayBlock;
switch (this.state.display) {
case 'SPORT':
displayBlock = <SportElement/>
break;
default:
displayBlock = (
<div className="container">
<div className="row">
<div style={ styles.xal_item_container } className="col-lg-2 col-md-3 col-sm-3 col-6">
<Link style={ Object.assign({}, styles.xal_item, styles.xal_red) } to={{
pathname: '/sport',
state: {
display: 'SPORT'
}
}}>
<div><i style={ styles.xal_icon } className="fa fa-money fa-5x"></i></div>
<div style={ styles.xal_title }>Sport</div>
</Link>
<Link to='/gg'>QWDWD</Link>
</div>
<div style={ styles.xal_item_container } className="col-lg-2 col-md-3 col-sm-3 col-6">
<div style={ Object.assign({}, styles.xal_item, styles.xal_red) }>
<div><i style={ styles.xal_icon } className="fa fa-money fa-5x"></i></div>
<div style={ styles.xal_title }>Crawler</div>
</div>
</div>
</div>
</div>
);
}
return (
<div className="container-fluid">
<div className="row">
<div style={ Object.assign({}, styles.xal_topBar, styles.no_margin) } className="col">
wwefwefe
</div>
</div>
<br/>
<div className="row">
<div className="col">
{ displayBlock }
</div>
</div>
</div>
);
}
};
function mapStateToProps(state) {
return {
};
}
function matchDispatchToProps(dispatch) {
return bindActionCreators({
}, dispatch);
}
export default withRouter(connect(mapStateToProps, matchDispatchToProps)(IndexPage))
class IndexPage扩展了React.Component{
建造师(道具){
日志(props.location.state);
超级(道具);
此.state={
显示:(props.location.state&&props.location.state.display)?props.location.state.display:null
};
}
componentDidMount(){
log(“[LOADED]IndexPage”);
const self=这个;
}
render(){
const self=这个;
var显示块;
开关(this.state.display){
“体育”一案:
显示块=
打破
违约:
显示块=(
运动
QWDWD
爬虫
);
}
返回(
wwefwefe
{displayBlock}
);
}
};
函数MapStateTops(状态){
返回{
};
}
功能匹配DispatchToprops(调度){
返回bindActionCreators({
},派遣);
}
使用路由器导出默认值(连接(mapStateToProps、matchDispatchToProps)(InExpage))
下面是App.js中的
:
import React from 'react';
import { Router, Redirect } from 'react-router';
import { browserRouter, Switch, Route } from 'react-router-dom'
import IndexPage from 'components/Pages/IndexPage.react'
export default class App extends React.Component {
render() {
return (
<Switch>
<Route exact path="/" component={IndexPage}/>
<Route exact path="/sport" component={IndexPage}/>
<Redirect from='*' to='/' />
</Switch>
);
}
}
从“React”导入React;
从“react Router”导入{Router,Redirect};
从“react router dom”导入{browserRouter,Switch,Route}
从“组件/页面/IndexPage.react”导入IndexPage
导出默认类App扩展React.Component{
render(){
返回(
);
}
}
我或多或少遇到了同样的问题。您需要将所有内容包装在BrowserRouter中:
export default class App extends React.Component {
render() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={IndexPage}/>
<Route exact path="/sport" component={IndexPage}/>
<Redirect from='*' to='/' />
</Switch>
</BrowserRouter>
);
}
}
导出默认类App扩展React.Component{
render(){
返回(
);
}
}
我或多或少遇到了同样的问题。您需要将所有内容包装在BrowserRouter中:
export default class App extends React.Component {
render() {
return (
<BrowserRouter>
<Switch>
<Route exact path="/" component={IndexPage}/>
<Route exact path="/sport" component={IndexPage}/>
<Redirect from='*' to='/' />
</Switch>
</BrowserRouter>
);
}
}
导出默认类App扩展React.Component{
render(){
返回(
);
}
}
您需要将应用程序组件放在单个路由器组件中。哦,很抱歉,您需要将应用程序组件包装在路由器中,而不是在扩展中。请忽略以上注释使用路由器导出默认值(应用程序)代码>不起作用您需要将应用程序组件放入单个路由器组件中。哦,很抱歉,您需要将应用程序组件包装在WithRouter中,而不是在Expage中。请忽略以上注释使用路由器导出默认值(应用程序)代码>不起作用