浏览器显示重定向的url,但未呈现组件-Redux Saga

时间:2019-04-29 13:18:52

标签: reactjs redux-saga

我正在尝试学习redux传奇。

我有一个编辑页面,提交表单后,应将其重定向到仪表板页面。

代码如下。

import { Switch, Redirect } from "react-router-dom";  
import { Router } from 'react-router';
import { Route } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';

const history = createHistory();
render()
    {
        return(
            <Router history={history}> 
                 <PrivateRoute exact path="/dashboard"  component={Dashboard}/>
               ...
            </Router>
        )
    }

更新用户的传奇如下。

import createHistory from 'history/createBrowserHistory';
const history = createHistory();



function* updateUserDetails(action)
{
    try {
        const response = yield call(userServices.updateUserDetails, action.payload)

        if(response.data && response.data.status === 'success') 
        {
            yield call(redirectToPage, '/dashboard');
        }
        else
        {
            yield put ({ type: actionTypes.UPDATE_USER_FAILURE});
        }
    }
}


function redirectToPage(location) {
    history.push('/dashboard');
}

问题是浏览器显示了重定向的URL,但未呈现组件。

关于如何解决此问题的任何想法。

1 个答案:

答案 0 :(得分:0)

我认为您应该只有一个history实例。尝试从第一个文件导出history对象,然后在第二个文件中导入它以代替使用。

import { Switch, Redirect } from "react-router-dom";  
import { Router } from 'react-router';
import { Route } from 'react-router-dom';
import createHistory from 'history/createBrowserHistory';

export const history = createHistory();
render()
    {
        return(
            <Router history={history}> 
                 <PrivateRoute exact path="/dashboard"  component={Dashboard}/>
               ...
            </Router>
        )
    }
import createHistory from 'history/createBrowserHistory';
import {history} from './App.js' //I assumed your first file is App.js



function* updateUserDetails(action)
{
    try {
        const response = yield call(userServices.updateUserDetails, action.payload)

        if(response.data && response.data.status === 'success') 
        {
            yield call(redirectToPage, '/dashboard');
        }
        else
        {
            yield put ({ type: actionTypes.UPDATE_USER_FAILURE});
        }
    }
}


function redirectToPage(location) {
    history.push('/dashboard');
}