can you give me simple example of zustand create store with function call api and get…
- User
can you give me simple example of zustand create store with function call api and get results and get that results in component
Assistant
Here's a simple example of using Zustand to create a store with an API call function and accessing the results in a React component:
// userStore.js import { create } from 'zustand' // Create store with API functionality const useUserStore = create((set) => ({ users: [], loading: false, error: null, // Function to fetch users from API fetchUsers: async () => { set({ loading: true, error: null }) try { const response = await fetch('https://jsonplaceholder.typicode.com/users') const data = await response.json() set({ users: data, loading: false }) } catch (error) { set({ error: error.message, loading: false }) } } })) export default useUserStore
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.