reactjs - 浏览器显示重定向的 url 但组件未呈现 - 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>
)
}
更新用户的传奇如下。
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');
}
推荐阅读
- ruby-on-rails - sidekiq 的任务是否有任何“组异步等待”?
- python - 按一列降序对 2by2 Numpy 数组进行排序
- visual-studio-code - VS Code 工具问题 - 在双显示器中显示空白
- java - 删除方法不删除 - Java
- r - 替换 data.table 中的表达式
- javascript - 为什么我需要按两次才能显示元素 0?
- rvest - 按值抓取类返回 `{xml_nodeset (0)}`
- python - 如何爆炸聚合的熊猫列
- python - django projects(saleor) python manage.py migrate 错误信息
- javascript - 在我的情况下使用 AsyncStorage 变量的方法是什么?