【问题标题】:Routing in ReactNative and ReactJS appReact Native 和 React JS 应用中的路由
【发布时间】:2018-01-14 01:19:14
【问题描述】:

我想使用 React 和 ReactNative 以及 React-Router 开发一个应用程序。 请帮助我在本机应用程序中组织路由,因为我是这方面的新手。

如果可以在 web 和 native 中使用相同的文件进行路由,那就太棒了。

这是为web part编写的sn-p代码:

import React from 'react';
import ReactDOM from 'react-dom';
import {Provider} from 'react-redux';
import {Router} from 'react-router-dom';
import history from './utils/History';

import store from './store/configureStore';
import Root from './containers/Root';

ReactDOM.render(
  <Provider store={store}>
    <Router history={history}>
      <Root/>
    </Router>
  </Provider>,
  document.getElementById('root')
);

如您所见,我为我的应用使用自定义历史记录,这就是“utils/History.js”文件的外观:

import createBrowserHistory from 'history/createBrowserHistory';

export default createBrowserHistory();

All 在网络浏览器中运行良好,但在 iOS 模拟器中却不行:

【问题讨论】:

    标签: reactjs react-native react-router-v4


    【解决方案1】:

    根据docs,您需要从react-router 包中导入Router,而不是react-router-dom

    import {Provider} from 'react-redux';
    import {Router} from 'react-router';
    import history from './utils/History';
    
    import store from './store/configureStore';
    import Root from './containers/Root';
    
    ReactDOM.render(
      <Provider store={store}>
        <Router history={history}>
          <Root/>
        </Router>
      </Provider>,
      document.getElementById('root')
    );
    

    【讨论】:

    • 感谢 Shubham,你给了我一个正确的方向!我将我的代码拆分为 web 和 native,并在这两种情况下都使用了 MemoryRouter。
    猜你喜欢
    • 2018-02-05
    • 1970-01-01
    • 2018-06-12
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 2019-03-17
    • 1970-01-01
    相关资源
    最近更新 更多