I'm still trying to understand this scenario. Can anyone suggest what is the correct way to do this in Next.js 13?
I display a list of users in a server component, for example like this (using MongoDB):
// UsersList.jsx const UsersList = () => { const users = await usersCollection.getUsers() return ( <div> {users.map(user) => <div>{user}</div>} </div> ) }
On the same page I also defined the client component for adding users:
// UsersEdit.jsx 'use client' const UsersEdit = () => { const handleAdd() => // calls POST to /api/users return // render input + button }
Both are displayed together in the server component page, as shown below:
// page.jsx const Users = () => { return ( <div> <UsersList /> <UsersEdit /> </div> ) }
How should I "reload" or "notify" UsersList
that a new user has been added to the collection to force it to show new/updated users?
https://stackoverflow.com/a/75127011/17964403 This is great for mutating on the client side, but if you want to do something like searching/filtering using input from the client, and want to re-fetch the same data, you can do something like
In the server component you will receive the search parameter as a prop, see if the search parameter exists, if it exists then pass that parameter in the fetch call and you will get the filtered items.
To reflect data updated by the client component on the server component, you can use
router.refresh()
, whererouter
isuseRouter()
. Here's an example of using a to-do list app:⚠️: The crawl request is cached. That's why
cache: 'no-store'
in this example.