2017-10-20 91 views
0

我有一個簡單的react-router-redux應用程序正在進行,其中/home有一個按鈕,點擊時應導航到/profile頁面。目前我的代碼看起來像這樣。反應路由器更改導航,但不使用REDX呈現頁面

動作/ index.js

import { push } from 'react-router-redux' 
import * as actionTypes from '../constants' 

const homeClicked =() => { 
    return { type: actionTypes.HOME_CLICK } 
} 

const profileClicked =() => { 
    return { type: actionTypes.PROFILE_CLICK } 
} 

export const handleHomeClick =() => { 
    return (dispatch) => { 
    dispatch(homeClicked()) 
    dispatch(push('/profile')) 
    } 
} 

export const handleProfileClick =() => { 
    return (dispatch) => { 
    dispatch(profileClicked()) 
    dispatch(push('/')) 
    } 
} 

容器/ HomeContainer.js

import React from 'react' 
import { connect } from 'react-redux' 
import * as actions from '../actions' 
import { withRouter } from 'react-router-dom' 
import PropTypes from 'prop-types' 

class Home extends React.Component { 
    handleClick =() => { 
    this.props.handleHomeClick(); 
    } 

    render() { 
    return (
     <div className='Home'> 
     <button onClick={this.handleClick}>Home</button> 
     </div> 
    ) 
    } 
} 

Home.propTypes = { 
    handleHomeClick: PropTypes.func.isRequired 
} 

const mapStateToProps =() => { 
    return {} 
} 

export default withRouter(connect(mapStateToProps, actions)(Home)) 

容器/ ProfileContainer.js

import React from 'react' 
import { connect } from 'react-redux' 
import * as actions from '../actions' 
import { withRouter } from 'react-router-dom' 
import PropTypes from 'prop-types' 

class Profile extends React.Component { 
    handleClick =() => { 
    this.props.handleProfileClick(); 
    } 

    render() { 
    return (
     <div className='Profile'> 
     <button onClick={this.handleClick}>Profile</button> 
     </div> 
    ) 
    } 
} 

Profile.propTypes = { 
    handleProfileClick: PropTypes.func.isRequired 
} 

const mapStateToProps =() => { 
    return {} 
} 

export default withRouter(connect(mapStateToProps, actions)(Profile)) 

減速器/ index.js

import { HOME_CLICK, PROFILE_CLICK } from '../constants' 
import { combineReducers } from 'redux' 
import { routerReducer } from 'react-router-redux' 

const clickReducer = (state={ message: 'HOME' }, action) => { 
    switch(action.type) { 
    case HOME_CLICK: 
    return { message: 'PROFILE' }; 
    case PROFILE_CLICK: 
    return { message: 'HOME' }; 
    default: 
    return state 
    } 
} 

export default combineReducers({ 
    clicking: clickReducer, 
    routing: routerReducer 
}) 

constants.js

export const HOME_CLICK = 'HOME_CLICK' 
export const PROFILE_CLICK = 'PROFILE_CLICK' 

history.js

import { createBrowserHistory } from 'history' 

export default createBrowserHistory() 

index.js

import React from 'react' 
import ReactDOM from 'react-dom' 
import { Provider } from 'react-redux'; 
import createRoutes from './routes' 
import rootReducer from './reducers' 
import thunk from 'redux-thunk' 
import browserHistory from './history' 
import reduxLogger from 'redux-logger' 
import { createStore, applyMiddleware } from 'redux' 
import { syncHistoryWithStore, routerMiddleware } from 'react-router-redux'; 

const middlewares = applyMiddleware(
    thunk, 
    routerMiddleware(browserHistory), 
    reduxLogger 
); 
const store = createStore(rootReducer, middlewares) 

const history = syncHistoryWithStore(browserHistory, store) 

const routes = createRoutes(history) 

ReactDOM.render(
    <Provider store={store}> 
    {routes} 
    </Provider>, 
    document.getElementById('root') 
) 

routes.js

import React from 'react' 
import { Router, Route, Switch } from 'react-router' 
import HomeContainer from './containers/HomeContainer' 
import ProfileContainer from './containers/ProfileContainer' 

const createRoutes = (history) => { 
    return (
    <Router history={history}> 
     <Switch> 
     <Route exact path='/' component={HomeContainer}/> 
     <Route path='/profile' component={ProfileContainer}/> 
     </Switch> 
    </Router> 
) 
} 

export default createRoutes 

app.js

import express from 'express' 
import config from './config' 
import path from 'path' 

const app = express() 

app.set('view engine', 'ejs'); 

app.use(express.static(path.join(__dirname, 'public'))) 

app.get('*', (req, resp) => { 
    resp.render('index'); 
}) 

app.listen(config.port, config.host,() => { 
    console.info('Server listening to', config.serverUrl()) 
}) 

此代碼更改URL,但點擊主頁上的home按鈕時,沒有渲染的個人資料頁面。還有一個link的redux記錄器輸出的圖片。

我被困在這個幾個小時了,其他的答案沒有太多的幫助。任何幫助,將不勝感激。

+0

你應該使用'react-router-dom'。他們仍然具有'Router'輸出,因此您不必使用'BrowserRouter'。 –

+0

等一下......你正在使用'react-router-dom'和'react-router'?這將如何工作?你的routes.js使用react-router,但其餘的應用使用react-router-dom。你應該修復'routes.js'文件以使用'react-router-dom'。 –

+0

如果您手動轉到地址欄中的「/ profile」,它是否有效?路由器工作,但不是按鈕,或者路由器根本不工作? –

回答

0

當你點擊家庭時,它應該渲染家庭路線,因爲你現在寫它?這不是它應該做的。

+0

不要看,'handleHomeClick'實際上會推送'/ profile',所以理想的情況是URL應該改變成'/ profile'(它的作用),並且渲染'ProfileContainer'(它不會)。我同意命名有點令人困惑,但'home'按鈕實際上重定向到'/ profile' – tauoverpi