SharePoint / Software Development / JavaScript / Office 365

SharePoint Framework and MobX – WebPart example

30 kwietnia, 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';

public updateColor(value: string): void {
   this.SelectedColor = value;

public updateIcon(value: string): void {
   this.SelectedIcon = value;

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: });
   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.

private onClick(event): void {;

And render of our main label.

<Label className={styles.mainLabel}>{}</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.

   () =>,
   async selectedItem => {
      const imageSrc: string = await ListService.getImage(selectedItem);;
   { 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
Ewelina Detkiewicz
Autor: Ewelina Detkiewicz
Inżynier ds. oprogramowania w centrum kompetencyjnym Microsoft 365 w Sii Polska. Na co dzień zajmuje się tworzeniem rozwiązań w technologii SharePoint Framework.

    Imię i nazwisko (wymagane)

    Adres email (wymagane)


    Treść wiadomości

    Zostaw komentarz