Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

131
Views
Next.js Zustand dynamic key addition to a state not working

I have been trying to add a key to a state dynamically in zustand. I have tried multiple ways like

1.

const store = (set, get) => ({
  keyAttrib: {key1: "value1", key2: 2},
  update: (key, value) => {
    let newState = { ...get().keyAttrib, [key]: value };
    set(newState);
  }
})
  1.   const store = (set) => ({
        keyAttrib: { key1: "value1", key2: 2 },
        update: (key, value) => {
          set((state) => ({ keyAttrib: { ...state.keyAttrib, [key]: value } }));
        },
      });
    

but none of them seem to be working.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The second example you provided works perfectly fine for me:

 const useStore = create((set) => ({
    keyAttrib: {key1: "value1", key2: 2},
    update: (key, value) =>
    set((state) => ({
      keyAttrib: {
            ...state.keyAttrib,
            [key]: value,
        },
    })),
  }));

Here's the working codesandbox (please ignore any styling) where you can add/update key-value pairs, the dropdown will always update key3. If this is still not helping, please provide a minimal wokring example on codesandbox.

event.preventDefault() is needed to prevent the form from performing a page refresh which will reset your Zustand Store with its initial value.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!