【问题标题】:react router changes navigation but does not render page using redux反应路由器更改导航但不使用 redux 呈现页面
【发布时间】:2018-04-01 23:09:39
【问题描述】:

我有一个简单的 react-router-redux 应用程序,其中/home 有一个按钮,单击该按钮应导航到/profile 页面。目前我的代码如下所示。

actions/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))

reducers/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())
})

当单击主页上的home 按钮时,此代码正在更改 url 但不呈现个人资料页面。还有一张link redux logger 输出的图片。

我被困了几个小时,其他 SO 答案并没有太大帮助。任何帮助将不胜感激。

【问题讨论】:

  • 您应该使用react-router-dom。他们仍然有Router 导出,因此您不必使用BrowserRouter
  • 等一下……你正在使用react-router-domreact-router??那将如何运作?您的routes.js 使用react-router,但应用程序的其余部分使用react-router-dom。您应该修复 routes.js 文件以使用 react-router-dom
  • 如果您手动转到地址栏中的/profile 是否有效?是路由器工作但按钮不工作还是路由根本不工作?
  • 是的,手动转到配置文件链接可以正常工作,并且可以正确呈现。
  • 单击按钮会更改 URL,但不会更改渲染。

标签: node.js reactjs redux react-router react-router-redux


【解决方案1】:

当您单击主页时,它应该呈现您现在编写的主页路线?这不就是它应该做的吗。

【讨论】:

  • 不看,handleHomeClick 实际上会推送/profile,所以理想情况下,url 应该更改为/profile(它确实如此)并呈现ProfileContainer(它没有)。我同意命名有点混乱,但home 按钮实际上重定向到/profile
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-04
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多