Rate this page:

Render element as React

renderElementAsReact is an utility provided to render custom content using React with the Client-side Extensions API.

It handles the mount/unmount cycles and rendering/unmounting the provided component for you. Also, it's recommended to use it since optimizations to React rendering content will be applied here in the future.

Signature

1
2
3
4
5
type renderElementAsReact = <T>(
    renderApi: PanelAPI | ModalAPI,
    RenderElement: ComponentType<{ options: unknown } & Omit<T, 'options'>>,
    additionalProps?: Omit<T, 'options'>,
) => void;

Arguments

NameTypeDescription
renderApi*PanelAPI | ModalAPIThe API object received in the onAction method should be provided as the argument. Only works for Panels and Modals.
RenderElement*ComponentTypeA React Component.
additionalPropsobject An optional object with additional props that will be bind to the component once mounted.

* required

Usage notes

1. Rendering a React component as content of a panel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { PanelExtension, renderElementAsReact } from '@atlassian/clientside-extensions';

import PanelContent from './panel-content';

/**
 * @clientside-extension
 * @extension-point reff.plugins-example-location
 */
export default PanelExtension.factory(() => {
    return {
        label: `React panel`,
        onAction(panelAPI) {
            renderElementAsReact(panelAPI, PanelContent);
        },
    };
});

2. Rendering a React component as content of an async panel with additional props

my-extension.ts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { AsyncPanelExtension } from '@atlassian/clientside-extensions';

const moduleLoader = () => import('./my-panel');

/**
 * @clientside-extension
 * @extension-point reff.plugins-example-location
 */
export default AsyncPanelExtension.factory(() => {
    return {
        label: `React async panel`,
        onAction: moduleLoader,
    };
});

my-panel.ts

1
2
3
4
5
6
7
8
9
10
interface Context {
    /*...*/
}

import { renderElementAsReact } from '@atlassian/clientside-extensions';
import MyPanelContent, { MyPanelContentProps } from './my-panel-content';

export default (panelAPI, context: Context) => {
    renderElementAsReact<MyPanelContentProps>(panelAPI, MyPanelContent, { context });
};

3. Rendering a React component as content of a modal with additional props

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { ModalExtension, renderElementAsReact } from '@atlassian/clientside-extensions';
import React from 'react';

interface Context {
    /*...*/
}

type ReactComponentProps = {
    context: Context;
    modalAPI: ModalExtension.Api;
};

const ReactComponent = ({ context, modalAPI }: ReactComponentProps) => {
    modalAPI.setTitle('An awesome modal with react');

    modalAPI.onClose(() => {
        /*...*/
    });

    modalAPI.setActions([
        /*...*/
    ]);

    return ( /*..Your custom modal content goes here..*/);
};

/**
 * @clientside-extension
 * @extension-point reff.plugins-example-location
 */
export default ModalExtension.factory((api, context) => {
    return {
        label: `Modal with react content`,
        onAction(modalAPI) {
            renderElementAsReact<ReactComponentProps>(modalAPI, ReactComponent, { modalAPI, context });
        },
    };
});

Rate this page: