Correct way to change state
Closed this issue · 9 comments
What is the correct way to change state property? I'm trying:
.delete({
action: 'deleteKey',
property: 'key',
path: ({ id,key }) => `/keys/${id}/${key}`,
onSuccess: (state, payload, axios) => {
// if you set the onSuccess function you have to set the state manually
state.keys = state.keys.filter(key => key.id !== id);
}
})
Hey @Tropicalista,
this is the correct way. Doesn't it work?
No it doesn't work...state does not change
Do you have any codesandbox template that I can use to show you my code?
I've created a simple example for you: https://codesandbox.io/s/6woq3156pn
Here's my code: https://codesandbox.io/s/oj247r3y9z
id
is not in the scope of onSuccess
. At the moment it's not easily possible to access the params. Please read here for more information: #79
If possible you could just return the id via the response and access it with the payload. Then you could do sth. like this:
onSuccess(state, payload, axios) {
const id = payload.data.id // assumes you are returning id in the response
state.posts = state.posts.filter(post => post.id != id);
}
I will find another solution.
Thanks!
No problem!
Please update vuex-rest-api to v2.10.0. I've added the {params, data}
as additional parameter in the onSuccess
and onError
functions so you can access now these properties.