使用react-redux获取URL有效但如果我刷新页面,它将无法工作

时间:2016-08-02 02:14:50

标签: node.js reactjs redux

我目前面临两个问题,我试图做一个基本的GET请求,它工作正常,但问题在于我刷新页面

为了您的信息,我正在做一个react-redux服务器渲染。

react-redux试图消费的网址是'/ courses'

问题

1)@ComponentScan是服务器的url和在react-redux中的提取,在导航方面它完全正常。例如,从'/courses'(主页)到'/'页面

它完美地显示了所有json数据,但是当我刷新页面时,它将显示所有json数据

示例

主页

enter image description here

课程页面

enter image description here

它显示完美,但是当我刷新页面时,它显示的所有json数据都不是来自react-redux。

enter image description here

我知道你们会说只需更改'/courses' - >的网址'/courses'我这样做了,我得到了第二个问题

2)哪个是

  

ReferenceError:每当我刷新页面时,都没有定义fetch

这是代码

路线

'/api/courses'

动作

import React from 'react';
import { IndexRoute, Route } from 'react-router';
import App from './components/App';
import Home from './components/Home';
import Course from './components/Course';

export default function getRoutes(store) {

  return (
    <Route path="/" component={App}>
      <IndexRoute component={Home}  />
      <Route path='/courses' component={Course}  />
    </Route>

  );


}

Main.js

export function getCourses() {
  return (dispatch) => {

    return fetch('/courses', {
      method: 'get',
      headers: { 'Content-Type': 'application/json' },
    }).then((response) => {
      if (response.ok) {
        return response.json().then((json) => {
          dispatch({
            type: 'GET_COURSES',
            courses: json.courses
          });
        })
      }
    });
  }
}

之后,我只是将动作发送到import 'whatwg-fetch'; import React from 'react'; import ReactDOM from 'react-dom'; import { Provider } from 'react-redux'; import { Router, browserHistory } from 'react-router'; import configureStore from './store/configureStore'; import getRoutes from './routes'; const store = configureStore(window.INITIAL_STATE); ReactDOM.render( <Provider store={store}> <Router history={browserHistory} routes={ getRoutes(store)} /> </Provider>, document.getElementById('app') ) 组件,该组件将呈现组件。

我对react-redux很新,希望你们能帮助我

0 个答案:

没有答案