浏览器显示重定向 url 但组件未呈现 - Redux Saga
Browser shows redirected url but component not rendered - Redux Saga
我正在努力学习 redux saga。
我有一个编辑页面,当提交表单时,它应该被重定向到仪表板页面。
代码如下
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>
)
}
更新用户的saga如下。
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 但组件未呈现。
关于如何解决这个问题的任何想法。
我认为您应该只有一个 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');
}
我正在努力学习 redux saga。
我有一个编辑页面,当提交表单时,它应该被重定向到仪表板页面。
代码如下
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>
)
}
更新用户的saga如下。
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 但组件未呈现。
关于如何解决这个问题的任何想法。
我认为您应该只有一个 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');
}