What is the method for arranging array elements in Zustand state?

type Serpent = {
    info: {
        id: string;
        isChosen: boolean;
        shade: string;
    }[],
    setChosenStatus:(prevInfo:{id:string, isChosen:boolean, shade:string}, isChosen:boolean) => void
}
  export const useSerpent = create<Serpent>((set) => ({
    info:[
        {id:'1', isChosen:false, shade:''},            
        {id:'2', isChosen:false, shade:''},
        {id:'3', isChosen:false, shade:''},
        {id:'4', isChosen:false, shade:''},            
        {id:'5', isChosen:false, shade:''},
        {id:'6', isChosen:false, shade:''},
        {id:'7', isChosen:false, shade:''},            
        {id:'8', isChosen:false, shade:''},
        {id:'9', isChosen:false, shade:''},
        {id:'10', isChosen:false, shade:''},            
        {id:'11', isChosen:false, shade:''},
        {id:'12', isChosen:false, shade:''},
        {id:'13', isChosen:false, shade:''},
        {id:'14', isChosen:false, shade:''},            
        {id:'15', isChosen:false, shade:''},
        {id:'16', isChosen:false, shade:''}
    ],

    setChosenStatus:(prevInfo:{id:string, isChosen:boolean, shade:string}, toToggle:boolean) => {
        
    }
  }))

i am trying to locate the object that matches prevInfo within the setChosenStatus properties and update its isChosen value with the toToggle property. However, I am uncertain about how to implement a map function inside the set() method.

Answer №1

modifySelection:(prevItem:{id:string, picked:boolean, shade:string}, selection:boolean) => {
    set(state => ({items: state.items.map(item => item === prevItem? {id:prevItem.id, picked:selection, shade:prevItem.shade}:item)}))
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I capture the click event on the oktext in Ionic?

When using Ionic, I have a select button with options for okText and cancelText. The issue I am facing is that when I click on okText, the menu closes as expected due to this attribute. However, I am interested in implementing it through click events. Belo ...

No call signatures found for TypeScript custom React hook

Trying out this new custom hook for API requests import { useState, useCallback } from 'react'; interface OptionsShape { method: 'GET' | 'POST'; } interface InitStateShape { data: any; success: boolean; loading: bool ...

Utilize Angular 9 to fetch data from an API using the Get method, map them to a class, and

Seeking to extract information from a test URL and convert the data into a list, I aim to exhibit them in an alert/Loop for testing purposes. The dummy API URL being used is: The returned data follows this structure: {"status":"success","data":[{"id":"1" ...

Combining several arrays to find the array of shared elements as the final result

In my JavaScript/TypeScript application built with Angular, I am facing the challenge of merging multiple arrays and extracting the common values that appear in all arrays. For example: arrayOne = [34, 23, 80, 93, 48] arrayTwo = [48, 29, 10, 79, 23] arrayT ...

Bidirectional binding with complex objects

In my Angular2 app, I have a class called MyClass with the following structure: export class MyClass { name: Object; } The name object is used to load the current language dynamically. Currently, for two-way binding, I am initializing it like this: it ...

What could be the reason for the defaultCommandTimeout not functioning as expected in my script

Is there a way to wait for only one particular element in Cypress without having to add wait commands everywhere in the test framework? I've come across the solution of adding defaultCommandTimeout in the cypress.json file, but I don't want it t ...

Bypass VueJs Typescript errors within the template section with Typescript

My VueJs App is functioning properly, but there is one thing that bothers me - a TypeScript error in my template block. Is there a way to handle this similar to how I would in my script block? <script setup lang="ts"> //@ignore-ts this li ...

Using Angular 5 to link date input to form field (reactive approach)

I'm encountering an issue with the input type date. I am trying to bind data from a component. Below is my field: <div class="col-md-6"> <label for="dateOfReport">Data zgłoszenia błędu:</label> <input type="date" formC ...

Unable to loop through array generated by service - potential async complication?

In my service, I am populating an array of items by calling a function like this: items: IItem[] = []; ... LoadItems() { this.GetItems().subscribe((res) => { // console.log(res); if (res.status == 200 && res.body. ...

Designing Buttons and Titles for the Login Page

I'm currently working on developing a straightforward login page in react native and I've encountered some difficulties with styling. Does anyone have tips on how to center the text on the button? Also, is there a way to move the INSTARIDE text ...

Instructions for adding a method to a prototype of a generic class in TypeScript

My current challenge involves adding a method to a prototype of PromiseLike<T>. Adding a method to the prototype of String was straightforward: declare global { interface String { handle(): void; } } String.prototype.handle = functi ...

The data type '{ [key: string]: number; }' cannot be assigned to type 'T'

I’m working with a complex TypeScript type and trying to manage it within a function. Here’s what I have: type T = { a: number } | { b: number } function func(k: 'a' | 'b', v: number) { // error message below const t: T = { ...

Tips for updating the checkbox state while iterating through the state data

In my component, I have the ability to select multiple checkboxes. When a checkbox is selected, a corresponding chip is generated to visually represent the selection. Each chip has a remove handler that should unselect the checkbox it represents. However, ...

Create an array variable for services in Angular

My goal is to define this user as an array. users = new BehaviorSubject<any>([]); In my component, I am attempting to add the userID to the array. this.Service.users.push(userID); To subscribe to it, I use the following code: this.Service.users.su ...

Explain what one-time typescript interfaces are

As someone who has been using React for quite some time, I am now looking to integrate Typescript into my projects. In the past, I would create large "container" objects like this: const theme = { colors: { primary: '#00f', accent: &ap ...

Angular 2: Implementing functionality on selected option

I have written code for my HTML <select [(ngModel)]="empfile.storeno" class="form-control"> <option *ngFor="let p of concept" [ngValue]="p.MAP_Code">{{p.MAP_Code}}</option> </select> As for my component public concept = []; ...

Error encountered: "Injection error: Angular2 + typescript + jspm : No provider found for Http ( App -> Provider -> Http )"

I am in the process of transitioning from Webpack to JSPM with system.js. Our application has a simple App component. I have been following the steps outlined in this article Angular 2 Starter Setup with JSPM, SystemJS and Typescript in atom (Part 1) impo ...

Changing the font family for a single element in Next.js

One unique aspect of my project is its global font, however there is one element that randomly pulls font families from a hosted URL. For example: https://*****.com/file/fonts/Parnian.ttf My page operates as a client-side rendered application (CSR). So, ...

Is it feasible to create a set of standardized values for an array's properties?

My goal is to restrict the values for a property (or react props in this case) based on the values provided in another property. Firstly, I have my Option interface: interface Option { value: string; label: string; } Next, I define my SelectInputProp ...

Can one obtain a public IP address using Typescript without relying on third-party links?

Though this question has been asked before, I am currently working on an Angular 4 application where I need to retrieve the public IP address of the user's system. I have searched on Stackoverflow for references, but most posts suggest consuming a th ...