Skip to content
Merged
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
55 changes: 55 additions & 0 deletions docs/apis/core/htmlwriter/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,61 @@ html_writer::start_span('zombie') . 'BRAINS' . html_writer::end_span();
// <span class="zombie">BRAINS</span>
```

### react_component {/* #react-component */}
Comment thread
andrewnicols marked this conversation as resolved.

<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.

:::note[Properties]

The `$modulename` must include the `@moodle/lms/` prefix in the module name.

The `$props` takes the data and JSON-encodes it. You should not pass a JSON-encoded value.

:::

The method outputs the `<div>` with two data attributes:

- `data-react-component` with the module name, including the full `@moodle/lms/` prefix;
- `data-react-props` containing JSON-encoded props.
Comment thread
andrewnicols marked this conversation as resolved.

:::caution[A note on React properties]
Comment thread
andrewnicols marked this conversation as resolved.

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

React properties **should not** contain additional 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
echo 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="{&quot;filter&quot;:&quot;latest&quot;,&quot;limit&quot;: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