The best way to use zustand global state management when patch request handling

I am using zustand for global state management with react and typescript. The question is how to handle the state update and reset effectively, for example when patch request scenarios where user might change input value but cancel that request. For example, let’s say we got useStore.ts like below:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import { create } from "zustand"
type StoreType = {
// 1. first set of values for existing user data from server
name: string | undefined
selectedNode: Node[] | undefined
// 2. Prepare new set of values for temporary
newName: string | undefined
newSelectedNode: Node[] | undefined
... (setState stuff)
}
export const useStore = create<StoreType>((set) => ({
name: undefined,
selectedNode: undefined,
newName: undefined,
newSelectedNode: undefined,
.... (setState stuff)
}))
</code>
<code>import { create } from "zustand" type StoreType = { // 1. first set of values for existing user data from server name: string | undefined selectedNode: Node[] | undefined // 2. Prepare new set of values for temporary newName: string | undefined newSelectedNode: Node[] | undefined ... (setState stuff) } export const useStore = create<StoreType>((set) => ({ name: undefined, selectedNode: undefined, newName: undefined, newSelectedNode: undefined, .... (setState stuff) })) </code>
import { create } from "zustand"


type StoreType = {
  // 1. first set of values for existing user data from server
  name: string | undefined
  selectedNode: Node[] | undefined

  // 2. Prepare new set of values for temporary
  newName: string | undefined
  newSelectedNode: Node[] | undefined

  ... (setState stuff)
}

export const useStore = create<StoreType>((set) => ({
  name: undefined,
  selectedNode: undefined,
  newName: undefined,
  newSelectedNode: undefined,

  .... (setState stuff)
}))

I am currently duplicating the backup stuff then assign the state value from global store so that we do not break exisiting data (like from server) when user cancel request or such.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> const {name} = useStore()
const [backupName, setBackupName] = useState<string>(name)
<Input
type="text"
value={backupResourceName}
placeholder={`Enter new name`}
onChange={(e) => setBackupName(e.target.value)}
/>
</code>
<code> const {name} = useStore() const [backupName, setBackupName] = useState<string>(name) <Input type="text" value={backupResourceName} placeholder={`Enter new name`} onChange={(e) => setBackupName(e.target.value)} /> </code>
  const {name} = useStore()
  const [backupName, setBackupName] = useState<string>(name)

  <Input
              type="text"
              value={backupResourceName}
              placeholder={`Enter new name`}
              onChange={(e) => setBackupName(e.target.value)}
            />

I feel it is not the efficient way since if we have several values, we need to create more backup – what is the typical handling of this kind of scenarios?

I came up just create newName global state within the zustand declaration like above but which is more common?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật