question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

POST action replaces entire data array (until refresh)

See original GitHub issue

When I do a POST action, the entire array of data (this.state.kittens.data below) is replaced by the new post data. When I do a browser refresh, the data is restored. Why?

reduxApi config:

export default reduxApi({

	kittens: {
		url: '/api/kittens/:id',
		crud: true,
		transformer: transformers.array,
		options: {
			headers: {
				'Accept': 'application/json',
				'Content-Type': 'application/json'
			}
		}
	}

}).use('fetch', adapterFetch(fetch));

POST action:

handleAdd (event) {
	const newKitten = {
		name: this.state.name,
	};
	this.props.dispatch(reduxApi.actions.kittens.post({}, { body: JSON.stringify(newKitten) }));
}

JSX:

<button onClick={this.handleAdd.bind(this)}>Add kitten</button>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lexichcommented, Oct 25, 2017

Good implementation 👍

0reactions
tomsoderlundcommented, Oct 25, 2017

Not sure if this is the right approach, but I solved it with a custom transformer:

transformer: function (data, prevData, action) {
	const actionMethod = _.get(action, 'request.params.method');
	switch (actionMethod) {
		case 'POST':
			return [...prevData, data];
			break;
		case 'PUT':
			return prevData.map(oldData => oldData._id === data._id ? data : oldData);
			break;
		case 'DELETE':
			return _(prevData).filter(oldData => oldData._id === data._id ? undefined : oldData).compact().value();
			break;
		default:
			return transformers.array.call(this, data, prevData, action);
	}
},

Also, I had to modify the server API to return the _id of the deleted item.

Thankful for any suggestions on how to improve.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Array only gets data when I refresh the page - Stack Overflow
since I can't see the full picture but it seems like something isn't coming in properly from the .api file. Can you console.log(categoryName) ......
Read more >
Refresh ag-Grid after a data change with React, Angular, Vue ...
This blogpost shows how to refresh ag-Grid after row adding/update/delete in all the popular web frameworks - Angular, React, VueJS.
Read more >
Cheat Sheet for Updating Objects and Arrays in React State
This is a cheat sheet on how to do add, remove, and update items in an array or object within the context of...
Read more >
How To Handle Async Data Loading, Lazy Loading, and Code ...
After the asynchronous function resolves, update an unordered list with the new information. Save and close the file. When you do the browser ......
Read more >
Array.prototype.fill() - JavaScript - MDN Web Docs - Mozilla
The fill() method changes all elements in an array to a static value, from a start index (default 0) to an end index...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found