问题描述
旧值回到输入字段.
我已经用输入字段值 this.state.rent(用户输入的值)初始化了 this.props.house.rent(这是旧的值)但是我不能这样做,因为它变成了反模式语法,你可以看到它在下面的代码注释中.
I had initialized this.props.house.rent (which is the old value coming back) with input field value this.state.rent (user entered value) but I cannot do it as it becomes antipattern syntax, you can see it in below comments of code.
cacheUpdate = (cache, { data: { updateHouse } }) => {
const data = cache.readQuery({
query: QUERY_CONFIRM_QUERY,
variables: { id: this.props.confirmId },
});
const houses = data.queryConfirm.houses;
const prevHouse = this.props.house;
//prevHouse.rent = this.state.rent; // this.state.rent is user entered input value
const updatedHouses = houses.map(house => {
if (house.id === prevHouse.id) {
const updatedHouseItem = _.pickBy(updateHouse, _.identity);
return { ...prevHouse, ...updatedHouseItem };
}
return house;
});
data.queryConfirm.houses = updatedHouses;
cache.writeQuery({
query: QUERY_CONFIRM_QUERY,
variables: {
id: this.props.confirmId,
},
data,
});
};
我希望从缓存的 readQuery 或 writeQuery 中删除旧值.
I want the old value to be removed from readQuery or writeQuery of cache.
推荐答案
希望这对某人有所帮助.Ooof..我想了很多,其实很简单.我想进行回调,但不可能,因为值来自数据库.无论如何,总而言之,在控制台中简单地查看 data.queryConfirm.houses 的值并分配所需的数组,例如说我需要的是 data.queryContact.houses[0].rent 并将其初始化为 this.state.rent.仅此而已.
Hope this helps someone. Ooof..I think a lot, it was simple actually. I thought to make callback, but not possible as values are coming from database. Anyways, in conclusion simple look at the value of data.queryConfirm.houses in the console and assign the required array like, say I required was data.queryContact.houses[0].rent and initialize it to this.state.rent. That's all.
这篇关于如何在 cache.readQuery 或 cache.writeQuery 中使用 this.state.rent 值而不是 this.props.house.rent 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!