Collections enable Arrays and Objects to be easily extended by pushing or unshifting new members. The namesake comes from Backbone Collections.
Collections were first introduced in @rest-hooks/[email protected]. @rest-hooks/rest@7 takes this one step further, but using them in Resource.getList.
Fixtures
GET /users
async response(...args){return(await UserResource.getList(...args)).slice(0,3);}
GET /todos
async response(...args){return(await TodoResource.getList(...args)).slice(0,7);}
PATCH /todos/:id
async response(...args){return{...(await TodoResource.partialUpdate(...args)),id:args?.[0]?.id};}
POST /todos
async response(...args){//await new Promise(resolve => setTimeout(resolve, 500));
return{...(await TodoResource.getList.push(...args)),id:randomId()};}
▶TodoResource
▶TodoItem
▶CreateTodo
import { useController } from '@rest-hooks/react/next';import { TodoResource } from './TodoResource';export default function CreateTodo({ userId }: { userId: number }) {const ctrl = useController();const handleKeyDown = async e => {if (e.key === 'Enter') {ctrl.fetch(TodoResource.getList.push, {userId,title: e.currentTarget.value,id: Math.random(),});e.currentTarget.value = '';}};return (<div className="listItem nogap"><label><input type="checkbox" name="new" checked={false} disabled /><input type="text" onKeyDown={handleKeyDown} /></label></div>);}
▶TodoList
Upgrading is quite simple, as @rest-hooks/rest/next
and @rest-hooks/react/next
were introduced
to allow incremental adoption of the new APIs changed in this release. This makes the actual upgrade a simple import
rename.
Other highlights include
@rest-hooks/rest
@rest-hooks/react