4.2.1 • Published 1 month ago

@microsoft/mgt-react v4.2.1

Weekly downloads
330
License
MIT
Repository
github
Last release
1 month ago

React wrapper for Microsoft Graph Toolkit

npm

Use mgt-react to simplify usage of Microsoft Graph Toolkit (mgt) web components in React. The library wraps all mgt components and exports them as React components.

mgt-react extends wc-react adding support for templates.

Installation

npm install @microsoft/mgt-react

or

yarn add @microsoft/mgt-react

Usage

Import a component at the top:

import { Person } from '@microsoft/mgt-react';

You can now use Person anywhere in your JSX as a regular React component.

<Person personQuery="me" />

Use properties instead of attributes

For example, you can set the personDetails property to an object:

const App = (props) => {
  const personDetails = {
    displayName: 'Bill Gates',
  };

  return <Person personDetails={personDetails}></Person>;
};

Register event handlers:

import { PeoplePicker } from '@microsoft/mgt-react';

const App = (props) => {
  const handleSelectionChanged = (e) => {
    this.setState({ people: e.target.selectedPeople });
  };

  return <PeoplePicker selectionChanged={handleSelectionChanged} />;
};

All properties and events map exactly as they are defined on the web component - see web component docs.

Templates

mgt-react allows you to leverage React for writing templates for mgt components.

Note: You can learn more about templating mgt components here

For example, to create a template to be used for rendering events in the mgt-agenda component, first define a component to be used for rendering an event:

import { MgtTemplateProps } from '@microsoft/mgt-react';

const MyEvent = (props: MgtTemplateProps) => {
  const { event } = props.dataContext;
  return <div>{event.subject}</div>;
};

Then use it as a child of the wrapped component and set the template prop to event

import { Agenda } from '@microsoft/mgt-react';

const App = (props) => {
  return <Agenda>
    <MyEvent template="event">
  </Agenda>
}

The template prop allows you to specify which template to overwrite. In this case, the MyEvent component will be repeated for every event, and the event object will be passed as part of the dataContext prop.

What components can I use?

The library is auto generated from the Microsoft Graph Toolkit and all components are available.

The names of the React components are in PascalCase and do not include the Mgt prefix. For example, the mgt-person component is available as Person, and the mgt-people-picker component is available as PeoplePicker. See the Microsoft Graph Toolkit documentation for a list of all components.

Why

If you've used web components in React, you know that proper interop between web components and React components requires a bit of extra work.

From https://custom-elements-everywhere.com/:

React passes all data to Custom Elements in the form of HTML attributes. For primitive data this is fine, but the system breaks down when passing rich data, like objects or arrays. In these instances you end up with stringified values like some-attr="object Object" which can't actually be used.

Because React implements its own synthetic event system, it cannot listen for DOM events coming from Custom Elements without the use of a workaround. Developers will need to reference their Custom Elements using a ref and manually attach event listeners with addEventListener. This makes working with Custom Elements cumbersome.

4.2.1

1 month ago

4.1.0

2 months ago

4.0.1

3 months ago

4.0.0

3 months ago

3.1.3

7 months ago

3.1.2

8 months ago

3.1.1

9 months ago

3.1.0

9 months ago

3.0.1

10 months ago

2.11.2

11 months ago

3.0.0

11 months ago

2.11.0

11 months ago

2.11.1

11 months ago

3.0.0-preview.2

1 year ago

2.10.1

1 year ago

3.0.0-rc.2

11 months ago

3.0.0-rc.1

11 months ago

3.0.0-rc.3

11 months ago

3.0.0-preview.1

1 year ago

2.10.0

1 year ago

2.9.2

1 year ago

2.9.1

1 year ago

2.8.0

1 year ago

2.9.0

1 year ago

2.7.0

2 years ago

2.7.1

2 years ago

2.6.2

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.5.0

2 years ago

2.5.2

2 years ago

2.5.1

2 years ago

2.4.0

2 years ago

2.3.2

2 years ago

2.3.1

2 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.1.0

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-rc.1

3 years ago

2.0.0-preview.9

3 years ago

2.0.0-preview.8

3 years ago

2.0.0-preview.6

3 years ago

2.0.0-preview.5

4 years ago

2.0.0-preview.4

4 years ago

2.0.0-preview.3

4 years ago

2.0.0-preview.1

4 years ago

1.3.0-preview.3

4 years ago

1.3.0-preview.2

4 years ago