2017-12-03 252 views
0

我使用mobx-react-router但我有一個問題,當我使用「推」的URL更改,但我的整個頁面變爲空白。所以我想也許我的組件沒有被渲染,或者它將進入一個全新的頁面。使用「推」,網址改變,但我的整個頁面變爲空白

"mobx": "^3.1.16", 
"mobx-react": "^4.2.2", 
"mobx-react-devtools": "^4.2.15", 
"mobx-react-router": "^4.0.1", 
"react": "^15.6.1", 
"react-dom": "^15.6.1", 
"react-router": "^4.1.1", 
"babel": "^6.23.0", 
"babel-core": "^6.25.0", 
"babel-loader": "^7.1.1", 
"babel-plugin-transform-class-properties": "^6.24.1", 
"babel-plugin-transform-decorators-legacy": "^1.3.4", 
"babel-preset-es2015": "^6.24.1", 
"babel-preset-react": "^6.24.1", 
"history": "^4.6.3", 
"html-webpack-plugin": "^2.29.0", 
"webpack": "^3.0.0", 
"webpack-dev-server": "^2.5.0" 

的WebPack

var HtmlWebpackPlugin = require('html-webpack-plugin'); 
    var path = require("path"); 
    module.exports = { 
     entry: './src/app.js', 
     output: { 
      path: path.resolve(__dirname, "dist"), 
      publicPath: "/", 
      filename: 'app.bundle.js' 
     }, 
     module: { 
      rules: [ 
       { 
        test: /\.s?css$/, 
        use: ['style-loader', 'css-loader', 'sass-loader'] 
       }, { 
        test: /\.js$/, 
        exclude: /node_modules/, 
        use: 'babel-loader' 
       }, { 
        test: /\.(ttf|otf|eot|svg|woff(2)?)(\?[a-z0-9]+)?$/, 
        loader: 'file-loader?name=fonts/[name].[ext]' 
       }, { 
        test: /\.(gif|png|jpe?g|svg)$/i, 
        loader: 'file-loader' 
       } 
      ] 
     }, 
     devServer: { 
      historyApiFallback: true 
     }, 
     plugins: [new HtmlWebpackPlugin({template: './src/index.html'})] 
    } 

App.js

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import { Provider } from 'mobx-react'; 
import { useStrict } from 'mobx'; 
import createBrowserHistory from 'history/createBrowserHistory'; 
import {syncHistoryWithStore } from 'mobx-react-router'; 
import { Router } from 'react-router' 

import AppContainer from './components/AppContainer'; 

const browserHistory = createBrowserHistory(); 

import stores from '../src/stores/Stores'; 

const history = syncHistoryWithStore(browserHistory, stores.routingStore); 


useStrict(true); 

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

Stores.js

import {RouterStore} from 'mobx-react-router'; 

const routingStore = new RouterStore(); 

const stores = { 
    routingStore, 
} 

export default stores; 

版本AppContainer

import React, {Component} from 'react' 
import ReactDOM from 'react-dom'; 
import {withRouter, Route} from 'react-router' 
import MainComponent from '../components/MainComponent' 
@withRouter 
export default class AppContainer extends Component { 
    constructor() { 
     super(); 
    } 
    render() { 
     return (
      <div> 
       <Route exact path='/' component={MainComponent}/> 
      </div> 
     ) 
    } 
} 

MainComponent

import React, {Component} from 'react' 
import ReactDOM from 'react-dom'; 
import {observer, inject} from 'mobx-react'; 
import {withRouter, Route} from 'react-router' 


import SecondComponent from './SecondComponent'; 
import ThirdComponent from './ThirdComponent'; 



@withRouter 
@inject('routingStore') 
@observer 
export default class MainComponent extends Component { 
    constructor() { 
     super(); 
    } 
    render() { 
    return (
      <div> 
       <h1>Main </h1> 
       <SecondComponent /> 
       <Route path='/test/third/:id/data' component={ThirdComponent}/> 

      </div> 
     ) 
    } 
} 

SecondComponent

import React, {Component} from 'react' 
import ReactDOM from 'react-dom'; 
import {observer, inject} from 'mobx-react'; 
import {withRouter, Route} from 'react-router' 


@inject('routingStore') 
@observer 
export default class SecondComponent extends Component { 
    constructor() { 
     super(); 
    } 
    render() { 
     const props = this.props; 
     const {push} = this.props.routingStore; 

    return (
      <div> 
       <h1>Second2 </h1> 
       <a onClick={() => {push(`/test/third/1/data`); }}>Render Third Component</a> 
      </div> 
     ) 
    } 
} 

ThirdComponent

import React, {Component} from 'react' 
import ReactDOM from 'react-dom'; 
import {observer, inject} from 'mobx-react'; 
import {withRouter, Route} from 'react-router' 



@inject('routingStore') 
@observer 
export default class ThirdComponent extends Component { 
    constructor() { 
     super(); 
    } 
    render() { 
    return (
      <div> 
       <h1>Third </h1> 
      </div> 
     ) 
    } 
} 
+0

你寫道你正在導入'withRouter',但你真的在使用它嗎?你的例子不清楚。我不是說這是問題,但很難說。 – Andy

+0

我現在已包含withrouter語句。我可能會稍後嘗試製作一個簡單的reactjs應用程序,以便我可以發佈我的問題,因爲很難將所有代碼混合到所有代碼中。 – chobo2

回答

1

這是因爲在你AppContainer,你已經加入日e exact財產到/路線(<Route exact path='/' component={MainComponent}/>)。

因此,當您更改URL以/測試/三/:ID /數據,路徑不再完全匹配/,所以MainComponent卸除。

刪除exact道具,這應該很好。

+0

嗯,這似乎工作在我發佈的例子,但在我的主要應用程序,我基本上基於我發佈的示例代碼,即使刪除「確切」後,我仍然得到同樣的問題。 – chobo2

+0

我想通了,我在路由中調用的組件中有一些其他錯誤。 – chobo2

相關問題