首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >history.push()和<重定向/>不工作

history.push()和<重定向/>不工作
EN

Stack Overflow用户
提问于 2019-12-24 10:03:52
回答 2查看 130关注 0票数 0

我尝试了大约5-7种不同的解决方案,关于重定向和历史不工作的反应路由器。这是创建-反应-应用程序代码:

index.js

代码语言:javascript
复制
import { Router} from 'react-router-dom';
import history from './History';
ReactDOM.render(<Router history={history}><App /></Router>, document.getElementById('root'));

History.jsx

代码语言:javascript
复制
import {createBrowserHistory} from 'history';
export default createBrowserHistory();

SplashScreen.jsx

代码语言:javascript
复制
return (
  <ViewContext.Provider value={this.state.contextChangeUtils}>
    <WrappedComponent/>
  </ViewContext.Provider>
);

App.jsx

代码语言:javascript
复制
static propTypes = {
  match: PropTypes.object.isRequired,
  location: PropTypes.object.isRequired,
  history: PropTypes.object.isRequired,
}
//...
const routes = [{
  path: '/',
  component: MainPage,
}, {
  path: '/status-history',
  component: HistoryPage,
}, {
  path: '/status-details',
  component: DetailsPage
}]

if (this.context.viewInfo.path!==this.state.lastPath) {
  this.setState({lastPath: this.context.viewInfo.path});
  let { history } = this.props;
  history.push(this.context.viewInfo.path);
}

return (
  <Container className="App pl-0 pr-0">
    <StatusHeader />
    <Row>
      <Col md="1" />
      <Col md="2" className="TagListOuter">
        <TagList tags={tags} />
      </Col>
      <Switch>
        {routes.map(r => (
          <Route path={r.path} key={r.path} component={r.component} />
        ))}
      </Switch>
    </Row>
  </Container>
);

App.contextType = ViewContext;

export default SplashScreen(withRouter(App));

  • 我尝试在每个路由的
  • 组件中使用this.props.history.push(),我尝试在我的应用程序(甚至index.js)

的所有级别使用一个简单的

有什么可能会出错的想法吗?提前感谢

EN

回答 2

Stack Overflow用户

发布于 2019-12-24 12:50:38

代码语言:javascript
复制
this.props.history.push({
        pathname: "Your  path",
        state: {
          lastPath: this.context.viewInfo.path
        }
});

您可以使用this.props.location.statethis.props.location.state.lastPath获取数据。

票数 0
EN

Stack Overflow用户

发布于 2019-12-24 12:57:13

如果您创建了一个自定义历史实例来管理您的响应路由器历史记录和调用函数,那么请按下面的方式使用它,而不是导入withRouter并将其注入组件。

代码语言:javascript
复制
import history from "../History.js"

static propTypes = {
  match: PropTypes.object.isRequired,
  location: PropTypes.object.isRequired,
  history: PropTypes.object.isRequired,
}
//...
const routes = [{
  path: '/',
  component: MainPage,
}, {
  path: '/status-history',
  component: HistoryPage,
}, {
  path: '/status-details',
  component: DetailsPage
}]

if (this.context.viewInfo.path!==this.state.lastPath) {
  this.setState({lastPath: this.context.viewInfo.path});

  history.push(this.context.viewInfo.path);
}

return (
  <Container className="App pl-0 pr-0">
    <StatusHeader />
    <Row>
      <Col md="1" />
      <Col md="2" className="TagListOuter">
        <TagList tags={tags} />
      </Col>
      <Switch>
        {routes.map(r => (
          <Route path={r.path} key={r.path} component={r.component} />
        ))}
      </Switch>
    </Row>
  </Container>
);

App.contextType = ViewContext;

export default SplashScreen(App);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59467065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档