Files
upstream-mirror 240db4e2a7
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 00:29:55 +05:00

5.7 KiB

title, sidebar_position
title sidebar_position
Build a component 4

This page describes how to build a new React component in the Mattermost web app. A new component must meet the following requirements:

  1. Is pure, meaning that all information required to render is passed in by props.
  2. Has no direct store interaction. Use connect to wrap the component if needed.
  3. Has component tests.
  4. Is generic and re-usable when possible.
  5. Has documented props.

If none of those make any sense to you or you're new to React and Redux, then check out these links:

These requirements are discussed in more detail in the following sections.

Design the component

The most important part of designing your component is deciding on what the props will be. Props are very much the API for your component. Think of them as a contract between your component and the users.

Props are read-only variables that get passed down to your component either directly from a parent component or from an index.ts container that connects the component to the Redux store.

How do you decide what props your component should have? Think about what your component is trying to display to the user. Any data you need to accomplish that should be part of the props.

As an example, let's imagine we're building an ItemList component with the purpose of displaying a list of items. The props for such a component might look like:

type Props = {
    /**
     * The title of the list
     */
    title?: string;
    
    /**
     * An array of items to display
     */
    items: ListItem[];
}

The title prop is a string that's displayed as the title of the list, and items is an array of objects that make up the contents of the list. Note that items is required while title is optional.

Make sure you add brief but clear comments to each prop type as shown in the example.

Our ItemList component would live in a file named item_list.tsx.

Use a Redux container component

The next question to ask yourself is whether you're going to need a container component. This is the index.ts file mentioned above. If your component needs either of the following, then you'll need a container:

  1. Needs some data injected into its props that the parent component doesn't have access to
  2. Needs to be able to perform some sort of action that affects the state of the store

Continuing the ItemList example above, maybe our parent component doesn't care about our list of items and doesn't have access to them. Let's also imagine that we want to let the user remove items from the list by clicking on them. This means our component now needs a container for both criteria above and our props will change slightly:

type Props = {
    /**
     * The title of the list
     */
    title?: string;
    
    /**
     * An array of item components to display
     */
    items: ListItem[];
    
    actions: {
        /**
        * An action to remove an item from the list
        */
        removeItem: (item: ListItem) => void;
    };
}

Note that the type definition for actions passed from Redux won't include the any reference to Redux's dispatch or Redux Thunk's getState. This is intentional as connect hides those details from the component.

The container will then handle getting data from the Redux state in mapStateToProps and passing Redux actions to the component using mapDispatchToProps. Note that either of these are optional if only one is needed.

import {connect} from 'react-redux';
import {bindActionCreators, Dispatch} from 'redux';

import {removeItem} from 'mattermost-redux/actions/items';
import {getItems} from 'mattermost-redux/selectors/entities/items';

import {GlobalState} from 'types/store';

import ItemList from './item_list';

function mapStateToProps(state: GlobalState) {
    return {
        items: getItems(state),
    };
}

function mapDispatchToProps(dispatch: Dispatch) {
    return {
        actions: bindActionCreators({
            removeItem,
        }, dispatch),
    };
}

export default connect(mapStateToProps, mapDispatchToProps)(ItemList);

If the selectors and/or actions you need don't yet exist in Redux then you should go add those first by following the guide to adding actions and selectors.

Your index.ts and item_list.ts files will live together in an item_list/ directory.

Implement the component

With the props defined and, if necessary, the container built, you're ready to implement the rest of your component. For the most part, implementing a component for the web app is no different than building any other React component. While older code tends to use class components which extend React.PureComponent, most newer code should use functional components.

Our ItemList example might look something like this:

type Props = {
    /**
     * The title of the list
     */
    title?: string;
    
    /**
     * An array of item components to display
     */
    items: ListItem[];
    
    actions: {
        /**
        * An action to remove an item from the list
        */
        removeItem: (item: ListItem) => void;
    };
}

export default function ItemList(props: Props) {
    const title = this.props.title ? <h1>{this.props.title}</h1> : null;
    const items = this.props.items.map((item: ListItem) => (
        <Item
            key={item.id}
            item={item}
            removeItem={this.props.actions.removeItem}
        />
    ));

    return (
        <div className='item-list'>
            {title}
            {items}
        </div>
    );
}

To test your component, follow the guide here.