Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/apis/core/htmlwriter/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,53 @@ html_writer::start_span('zombie') . 'BRAINS' . html_writer::end_span();
// <span class="zombie">BRAINS</span>
```

### react_component {/* #react-component */}

<Since issueNumber="MDL-89296" version="5.3" />

```php
html_writer::react_component(
string $modulename,
array|string|\stdClass|\JsonSerializable $props,
);
```

Render a placeholder `<div>` for a React component mount point.

The method writes:

- `data-react-component` with the module name;
- `data-react-props` containing JSON-encoded props.

:::caution[A note on React properties]

For React components we **strongly** encourage you to limit the properties to only the initial configuration and preferences.

React properties **should not** contain any data.

Historically it was often necessary to include a range of additional properties when writing Mustache template context properties because of limitations in the way that Mustache works. For example it was often necessary to provide certain strings, as well as some hard-to-fetch data. These limitations are not present in React.

:::

Example usage:

```php
html_writer::react_component(
'@moodle/lms/core/example',
(object) [
'filter' => get_user_preference('tool_demo/widget_filter', 'all'),
'limit' => get_user_preference('tool_demo/widget_result_limit', 25),
],
);
// <div data-react-component="@moodle/lms/core/example" data-react-props="{\"filter\":\"latest\",\"limit\":\"25\"}"></div>
```

:::note

If you are rendering a `\core\output\react_component_renderable` via `renderer_base::render()`, Moodle calls this method for you.

:::

### Generic tags {/* #generic-tags */}

```php
Expand Down
Loading
Loading