3

使用 Link 时,路由器按预期工作,但我收到警告 [history] ​​pushState is deprecated;改用推。

使用 react-router-redux 中的 routeActions 不起作用,url 已更改(http://localhost:3002/addCity),但如果我通过 url 转到页面,则查看仍然相同(主页)或显示错误:localhost :3002/添加城市。

混帐:https ://github.com/kirsanv43/Weather.git

减速机:

export default combineReducers({
  cities,
  routing: routeReducer
});

存储配置:https ://github.com/kirsanv43/Weather/blob/master/app/redux/config/store.js

import rootReducer from '../reducers'

export default function configureStore(initialState) {
  const history = createHistory();
  const middleware = syncHistory(history)

  const finalCreateStore = compose(
    applyMiddleware(middleware)
  )(createStore)

  const store = finalCreateStore(rootReducer)

  middleware.listenForReplays(store);

  if (module.hot) {
    // Enable Webpack hot module replacement for reducers
    module.hot.accept('../reducers', () => {
      const nextRootReducer = require('../reducers').default
      store.replaceReducer(nextRootReducer)
    })
  }

  return store
}

路由器:

const store = configureStore()

ReactDOM.render(
  <Provider store={store}>
    <Router history={browserHistory}>
      <Route path="/" component={App}>
        <IndexRoute component={Home}/>
        <Route path="addCity" component={AddCity}/>
      </Route>
    </Router>
  </Provider>
  ,
  document.getElementById('root')
);

零件:

class CitiesList extends React.Component { 
  onAddCity = () => { 
    this.props.route.push('/addCity')
  };

  render() {

    return <div className="weatherContainer">
      <ul>
        <li className="listItem addBtn"><a onClick={this.onAddCity}><span>ADD CITY</span></a></li>
        </ul> 
    </div>
  }

}

function mapDispatchToProps(dispatch) {
  return { 
    route:bindActionCreators(routeActions, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(CitiesList)
4

2 回答 2

0

请尝试将“react-router-redux”版本更改为“^5.0.0-alpha.8”。它解决了我的问题

于 2018-02-09T08:19:50.690 回答
0

insted createHistory 需要使用'history'中的createHashHistory

对我有用

于 2016-02-07T15:24:11.173 回答