SharePoint / Software Development / JavaScript / Office 365

SharePoint Framework and MobX – WebPart example

Kwiecień 30, 2019 0
Podziel się:

We know how MobX can make managing state of our SPFx app easier and what are its fundamentals. So let’s see how to put all that knowledge into practice.

We’re going to focus on example from the first article. Just for the record:

SPFx MobX 1 Mockup Office 365 - SharePoint Framework and MobX – WebPart example

A mockup of an example solution

After creating basic SPFx WebPart, we can install MobX. In our case, we can use npm – we need two packages:

And to make our lives easier – ready-to-use components from Office UI Fabric.

With MobX onboard, we can create our store class. We’re going to share some observables, so let’s create actions as well.

export class ExampleStore {
@observable public SelectedColor: string = '#f03333';
@observable public SelectedIcon: string = 'FavoriteStar';
@observable public SelectedItem: string = 'This is the first option';

@action.bound
public updateColor(value: string): void {
   this.SelectedColor = value;
}

@action.bound
public updateIcon(value: string): void {
   this.SelectedIcon = value;
}

@action.bound
public updateItem(value: string): void {
   this.SelectedItem = value;
}
}

We have to make sure, that our components share the same instance of the store, so let’s create it in the main WebPart class and distribute it to the rest of elements.

export default class SpfxMobxExampleWebPart extends BaseClientSideWebPart<ISpfxMobxExampleWebPartProps> {
private store: ExampleStore = new ExampleStore();

public render(): void {
   const element: React.ReactElement<IMainContainerProps> = React.createElement(
    MainContainer, { store: this.store });
   ReactDom.render(element, this.domElement);
}
…
}

Useful tip for future. If few solutions or webparts are using MobX, they load it separately to the target site. The same situation happens when user add solution provided by another supplier. It can use MobX as well and every instance of MobX can interfere with other. We should always protect our solution against this type of issue. I recommend to use isolateGlobalState flag:

configure({ isolateGlobalState: true });

You can learn more about it here.

It’s time for our component structure.

  • Main Container
    • Top Label
    • Middle Container
      • Dropdown #1
      • Dropdown #2
    • Bottom Container
      • Left Column
        • Row #1
        • Row #2
        • Row #3
      • Right Column Image

We assumed that the row icon is connected with the top label. So let’s look at ‘click’ handler of the first one.

@autobind
private onClick(event): void {
   this.props.store.updateItem(this.props.text);
}

And render of our main label.

<Label className={styles.mainLabel}>{this.props.store.SelectedItem}</Label>

Click on the icon changes store’s observable that is used by the top label. Yaay! How about some reactions?

Rows suppose to make influence on the image column on the right. Let’s say that selection of the item causes sending request to SharePoint list to get the picture source. It’s a great opportunity to use MobX reactions.

reaction(
   () => this.props.store.SelectedItem,
   async selectedItem => {
      const imageSrc: string = await ListService.getImage(selectedItem);
      this.props.store.updateImageSrc(imageSrc);
   },
   { fireImmediately: true }
)

Our function reacts to the item changes – it triggers a service and updates the image source. You can learn more about reactions here.

So, finally, we end up with our MobX based WebPart.

SPFx MobX 3 Clicking Office 365 - SharePoint Framework and MobX – WebPart example

Basic operations on our WebPart

To sum up our article series:

  • MobX is a library that make state management of SPFx app easier – it helps you share data between components
  • It was created with the intention to use it with React
  • Main elements of MobX based SPFx solution could be:
    • the store class with observables, computed values and actions
    • observer classes
    • reactions
5 / 5
Ewelina Detkiewicz
Autor: Ewelina Detkiewicz
Cześć! Jestem młodszym inżynierem ds. oprogramowania w centrum kompetencyjnym Office 365 w Sii Polska. Na co dzień zajmuję się tworzeniem rozwiązań w technologii SharePoint Framework.

Imię i nazwisko (wymagane)

Adres email (wymagane)

Temat

Treść wiadomości

Zostaw komentarz