I'm trying to change the state of redux from my reducer after AJAXing a server, but for some reason when I change my state variable in the promise response it still doesn't change the state like I want to.
I mad my own interface for AXIOS:
export function my_post(url, json_data, success_callback, error_callback) {
return axios({
method: 'post',
url: url,
data: btoa(JSON.stringify(json_data)) || {},
headers: {
'X-XSRFToken': getCookie('_xsrf')
}
}).then(response => {
if (success_callback)
success_callback(response)
}).catch(error => {
if (error_callback)
error_callback(error)
})
}
I'm calling this function in my reducer like this:
export default (state = initState, action) => {
switch(action.type) {
case actionTypes.AJAX:
my_post(
'/ajax/url',
{
content: 'some content'
},
(response) => {
state = {
...state,
ts: response['ts']
}
}
)
break
}
return state
}
also tried this:
export default (state = initstate, action) => {
switch(action.type) {
case actionTypes.AJAX:
my_post(
'/ajax/url',
{
content: 'some content'
}
).then(
state = {
...state,
ts: response['ts']
}
)
break
}
return state
}
but when I get to the return state
the ts argument doesn't exist.
What am I missing here?