Re-invoke 'store.get' after calling 'store.push'
ga-reth opened this issue · comments
Hi, thanks a lot for this, it's exactly what I needed for my project.
I have one query....
After adding a new item to an array of objects, how can I force store.get to re-invoke? This would re-render my list of data to reflect the changes.
And how can I add res to state for rendering?
onLoad = async () => {
store.get('array')
.then((res) =>
console.log(res)
);
}
saveData = () => {
const { title } = this.state;
const data = {
title
};
store.push('array', data);
}`
Thanks in advance
There's no built in way to get the current array values from .push
. In your saveData
function you could add a get after pushing at setState
after that to force a re-render.
saveData = async () => {
const { title } = this.state;
const data = {
title
};
await store.push('array', data);
const array = await store.get('array');
this.setState({ array });
};
I'm not exactly sure what your aim is with the onLoad
function, but you could setState
values from array
in that function as well to force a re-render.
onLoad = async () => {
const array = await store.get('array');
this.setState({ array });
};
Or, if you wanted to combine those functions to save a little space.
setArrayInState = async () => {
const array = await store.get('array');
this.setState({ array });
};
saveData = async () => {
const { title } = this.state;
const data = {
title
};
await store.push('array', data);
this.setArrayInState();
};
Great, thanks a lot for your help.