gpt4 book ai didi

javascript - 刷新同一页面而不重新加载 - Reactjs

转载 作者:行者123 更新时间:2023-11-29 15:58:06 25 4
gpt4 key购买 nike

如何在不重新加载的情况下刷新页面,通过刷新我想调用 componentDidMount() 函数并再次渲染 View 。

以下代码在重定向到不同的 URL 时有效,但如果我们重定向到相同的 URL 则无效。

props.dispatch(push('/sameUrl'));

最佳答案

我有一个类似的问题,但我在我的项目中使用了 redux,在我得到一个元素后,我刚刚通过'redux-saga/effects'和'connected-react-router'包更新了我的页面,我设置了一个例子(我削减了大部分代码,只留下了需要的代码):

当我发送创建食谱的请求时:

import { takeLatest, all, call, put } from 'redux-saga/effects';
import { push } from 'connected-react-router';
import * as constants from './RecipesConstants';
import { recipeAPI } from './../../api/';

function* addRecipe(action) {
try {
const recipeResponse = yield call(recipeAPI.addRecipe, action.payload);


yield put({
type: constants.ADD_RECIPE_SUCCESS,
payload: {
...recipeResponse.data
}
});

yield put(push(`/recipes`)); // <----- here i update my page
} catch (error) {
yield put({
type: constants.ADD_RECIPE_FAILED
});

}
}
export default function* recipesSaga() {
yield all([
takeLatest(constants.ADD_RECIPE, addRecipe)
])
}

希望对你有帮助

关于javascript - 刷新同一页面而不重新加载 - Reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56611396/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com