remix-run / react-router-website

The React Router website
https://reactrouter.com
MIT License
152 stars 35 forks source link

Tutorial: contactLoader returns a wrapped object, not clear why #112

Closed zspitz closed 1 month ago

zspitz commented 3 months ago

The first loader introduced in the tutorial returns contacts wrapped in an object.

export async function loader() {
  const contacts = await getContacts();
  return { contacts };
}

which is then destructured from useLoaderData:

const { contacts } = useLoaderData();

Why not return contacts directly from the loader?


Same for creating a contact, getting a contact

brookslybrand commented 1 month ago

@zspitz it's just a preference, you can do either one.

Many people like to return an object because it's easy to add more data later

By the way, if you have feedback or corrections to the docs, the source is in the remix repo