【发布时间】:2020-05-21 08:14:45
【问题描述】:
我有这个问题,当我对某些数据进行插入或更改时,要查看需要重新加载页面的新数据,而我会自动更新值而无需重新加载页面。我该怎么办?
这是用户点击提交和发帖的部分
_onSubmit(Document)
{
const self = this
if ( !_.isEmpty(Document) )
{
//..
if (Document && !_.isEmpty(Document.Anagraphics))
{
alertify.confirm(
utility.t('sureYouWanna_SAVE'),
() => {
const now = new Date();
Document._id = `PRODUCT:${new Date().getTime()}-${utility.CUID()}`
Document.CreationDate = now.toISOString()
Document.CategoryCode
Document.Status = 'New';
Document.Type = 'PRODUCT';
self._POST(Document)
},
function(){}
).set('labels', {ok: utility.t('YES_SAVE'), cancel: utility.t('CANCEL')})
}
else
{
$methods.WarnMissingValues()
}
}
else {
$methods.WarnMissingValues()
}
}
_POST(Document)
{
console.log("DOCUMENT POST", Document)
const self = this
const auth = this.props.db.auth
fetch(`${this.props.db.couch_db_host_url}requests`,{
method: 'POST',
credentials: 'include',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'Basic ' + btoa(`${auth.username}:${auth.password}`)
},
body: JSON.stringify(Document)
})
.then(response => {
alertify.dismissAll()
if(response.status > 299 || response.status < 200){
alertify.error(utility.t('AN_ERROR_OCCURRED'))
self._updateState({ submitSucceeded: false })
}
else{
alertify.alert(utility.t('ITEM_EDITED_OK'), function(){})
self.props.history.push({
pathname: RoutesIT.products_details
})
}
})
.catch((err, warning) => {
if (err)
{
alertify.dismissAll()
alertify.error(utility.t('AN_ERROR_OCCURRED'))
console.log('_POST', err);
self._updateState({ submitSucceeded: false })
}
else
{
console.log(warning)
alertify.dismissAll()
alertify.warning(utility.t(warning))
}
})
}
如何不重新加载页面以查看帖子的结果?谢谢
更新:
在页面中我也有:
function mapStateToProps(state) {
const { app: { login, p, c, l, c_timestamp, p_timestamp, l_timestamp }, form } = state;
return {
db: login ? login.db : null,
Sender: login ? login.Location : null,
timestamp: login ? login.timestamp : null,
[ FORM_NAME ]: form[FORM_NAME],
products: p,
locations: l,
categories: c,
categories_timestamp: c_timestamp,
products_timestamp: p_timestamp,
locations_timestamp: l_timestamp,
utente: login,
};
}
减速器
case actions.CATE_UPDATE:
{
return {
...state,
c: action.payload,
c_timestamp: new Date().getTime()
}
}
【问题讨论】:
-
您也需要将数据添加到状态中,在您将项目发布到 api 之后,从您的示例代码中,不清楚您如何存储现有数据,我无法提供工作例子。
-
如果你使用 HTML 表单的 post 方法,浏览器会强制重新加载,你必须在 onSubmit 事件中返回 false。我认为您应该使用 ajax 将数据发送到服务器,页面不会重新加载,并且您无法接收回调 ajax 的响应。
-
我使用 redux 保存状态可能是这个问题吗?
-
我不明白该怎么做..在你看来我应该看到哪一部分?
-
你说你使用 Redux,对吧?你介意发布你的reducer的代码,你正在更新数据的部分吗?减速器通常是此类错误的根源,因此它是开始寻找问题的好地方。
标签: javascript reactjs