Loading...

State management simplified with zustand

State management simplified with zustand

zustand is a small, fast and scalable state management package that you can use with your react applications, it can be used as your data store within your PCF control.

image.png

Github link

Live Demo here

Below are the benefits of using it,

  1. Bundle size is 1.17 kb!
  2. Avoid props inheritance between your components
  3. Simplify the functions implementations for maintaining data across the control.
  4. Provides hooks to use the data store which handles re-renders
  5. Store can be called outside functional components

    In our case in index.ts to get data in the getOutputs method

Check how to use it below,

Creating your data store

After initiating your control, create your store

//if you're using typescript, it would be better to set the type of the store
type PcfState= {
  vars: any[]; //data object
  add: (newVar: any) => void; //add function
  set: (vars: any[]) => void; //set function
  update: (_var: any) => void; //update function
  remove: (schema: number) => void; //remove function
  clear: () => void; //clear function
};
// the store below, holds the data objects and the functions related
const usePcfStore = create<PcfState>((set, get) => ({
  vars: [],
//get().vars returns the current state of vars array
//set() sets the state of the store properties
  add: (newVar: any) => set({ vars: [...get().vars, newVar] }),
  set: (vals: any) => set({ vars: vals }),
  update: (_var: any) =>
    set({
      vars: get().vars.map((_obj: any) => {
        if (_obj.id === _var.id) {
          _obj = _var;
        }
        return _obj;
      }),
    }),
  remove: (id: number) =>
    set({
      vars: get().vars.filter((x) => x.id !== id),
    }),
  clear: () => set({ vars: [] }),
}));
export default useVarsStore;

Use the store hook in your component

const { add, set, vars, remove, update } = usePcfStore();

You can easily use the data store functions just like that

set(newVars);

and you get the data object like that

let _obj = vars; //just like that

image.png

Get the store data in the getOutputs method

image.png

//easy as this!
public getOutputs(): IOutputs {
    return {
      bindProperty : usePcfStore.getState().vars
    };
  }

Hope this makes it easier for you in your PCF development.

Published on:

Learn more
Vite apps
Vite apps

Vite apps

Share post:

Related posts

Fast configuring security roles for your Dataverse project

If you're working on a Dataverse project specially without Dynamics 365 apps, you can make it easy to configure security roles for the project...

3 years ago

Display Omnichannel chat widget in the footer of your portal

The combination of PowerPortals, Dynamics 365 Omnichannel and Power Virtual Agents increases the level of customer engagement for customers, i...

3 years ago

Process & visualize Dynamics 365 marketing data

When Marketing emails and journeys are active, you start seeing the analytics in Dynamics 365 for Marketing, like the below The analytics are...

3 years ago

What is powerapps-modals?

Overview powerapps-modals gives you the tool to build attractive modals in Model-driven apps fast & easy. You just need to configure the m...

3 years ago

How to use powerapps-modals

In this blog, I'm writing some examples that power-apps modals can be used. 🧑💻 Source code Main function to call modals here function displa...

3 years ago

Tailwind CSS for your PCF control

What is TailwindCSS? Tailwind CSS is a utility-first CSS framework packed with classes like that can be composed to build any design, directly...

3 years ago

Combo Box Control

Intro ComboBox control is an auto-complete dropdown where users are able to search and navigate between options. For installation follow the ...

3 years ago

Radio Group

Intro Radio group control is a control that represents single select options in different radio representations For installation follow the l...

3 years ago

Advanced Text Area

Intro Advanced Text Area is a control that speeds up creating records. Why advanced? The control can be configured to let users attach files ...

3 years ago

Improve site speed in PowerPages

What is CDN? In short, Content Delivery Network (CDN) caches your public assets of your site in distributed servers, so if you have a website ...

3 years ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy