Storage con Zustand

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.

import { create } from 'zustand'
import { persist } from 'zustand/middleware'

const useStudentStore = create(
        persist(set => ({
                studentId: null,
                actions: {
                        setStudentId: (studentId) => set(state => ({ ...state, studentId })),
                }
        }), {
        name: 'termine', // Nombre que se utilizará en el localStorage
        partialize: (state) => ({ studentId: state.studentId }), // Aquí ponemos lo que se guardará en el localStorage. Es recomendable especificarlo expresamente, porque si no lo hacemos, se guardará todo el contenido de la store en el localStorage, lo cual incluye las funciones de los actions, pero como no podemos guardar una función en el localStorage, daría problemas
}))

export default useStudentStore;

// Actions
export const useStudentStoreActions = () => useStudentStore((state) => state.actions);
import useStudentStore from '../store/student';
import { useStudentStoreActions } from '../store/student';

const Student = () => {

    const { studentId } = useStudentStore();
    const { setStudentId } = useStudentStoreActions();
    return (
        <div>
            {studentId}
            <button onClick={() => setStudentId(5)}>Pulsar</button>
        </div>
    )
}

export default student;

Por 9.99€ al mes tendrás acceso completo a todos los cursos. Sin matrícula ni permanencia.