I'm new to React and using Zustand to handle global stores. My goal in this particular instance is to route to an "RSVP" page if the correct user is found.
Scenario A: User is found in the database. Route to new page
Scenario B: User is not found in the database. Stay on current page and display error message
It seems straightforward if you want to achieve this on click, however I can't find any information on how to trigger it within a state.
My understanding is that useHistory can be used, but only within a function, so it doesn't work when I try to use it in a state. eg.
import create from 'zustand';
import { mountStoreDevtool } from 'simple-zustand-devtools';
import { useHistory } from "react-router-dom";
const useStore = create((set, get) => ({
guests: [],
currentGuests: [],
message: "Please enter your unique password found in your email.",
returnDatabase: (input) => {
const guests = get().guests;
const targetParty = guests.filter((guest) => guest.party === input);
if (targetParty.length !== 0) {
set({ currentGuests: targetParty });
const setMessage = "Please enter your unique password found in your email.";
set({ message: setMessage });
//I want to programatically redirect here
let history = useHistory();
history.push("/rsvp");
} else {
const setMessage = "Your password has not been found. Please check your email and try again.";
set({ message: setMessage });
}
},
setStore: (data) => {
const guestsData = data.guests;
set({ guests: guestsData });
},
}));
However, I'm receiving this error:
Failed to compile
src/store/storeUtil.js
Line 19:21: React Hook "useHistory" is called in function "returnDatabase" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter react-hooks/rules-of-hooks
Search for the keywords to learn more about each error.
I figure this is because it's not in a function, so I tried to put it in a separate component:
import create from 'zustand';
import { mountStoreDevtool } from 'simple-zustand-devtools';
import { useHistory } from "react-router-dom";
export function RsvpButton(){
let history = useHistory();
function handleClick() {
history.push("/rsvp");
}
return (
{handleClick}
);
}
const useStore = create((set, get) => ({
guests: [],
currentGuests: [],
message: "Please enter your unique password found in your email.",
returnDatabase: (input) => {
const guests = get().guests;
const targetParty = guests.filter((guest) => guest.party === input);
if (targetParty.length !== 0) {
set({ currentGuests: targetParty });
const setMessage = "Please enter your unique password found in your email.";
set({ message: setMessage });
//try to use component here
<RsvpButton />
...etc
And this just doesn't do anything/have any errors. Should I be using a different method to route to the RSVP page other than useHistory? Or am I doing something wrong in my implementation?