英文:
How to use Context API in Next JS 13 with server-side components?
问题
在Next.js 13中,是否可以在服务器上获取数据,然后将其传播到Context API?
在我的情况下,我想通过令牌在服务器上获取用户信息,然后将其传递给Context API,因为我可能会在应用的其他地方使用用户数据。问题在于Context API是客户端技术,所以无论我想在哪里使用它,最终都要声明具有'use client'的客户端组件。是否有可能兼顾这两个世界的优势?
英文:
Is it possible in Next JS 13 to fetch data on the server but then propagate it to Context API?
In my case, I want to fetch user information via token on the server, but then pass it to Context API since I may use user data somewhere else in the app. The problem is that Context API is a client-side technology, so wherever I want to use it I end up declaring client components with 'use client'. Is it possible to get the best of two worlds?
答案1
得分: 1
无法在ServerSide组件中使用上下文。您可以使用zustand
来创建不需要将元素包装在提供程序内的全局状态。
英文:
It is not possile to use context with ServerSide component. You can use zustand
for creating global state that does not require elements to be wrapped inside a provider.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论