Skip to content

Commit fa9c0a2

Browse files
committed
add more docs
1 parent e4c0102 commit fa9c0a2

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

docs/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,36 @@ attribute to determine the order in which the handlers are called.
490490
| `HandleRequestInterface&PostProcessInterface` | Transform a response before it is sent to the client |
491491
| `RequestDeterminatorInterface` | Determine if the request is an API/Htmx/Browser request |
492492
| `ExceptionHandlerInterface` | Handle unhandled exceptions |
493+
494+
## Children
495+
496+
There is a special `Children` component that will inject the children of the current component into the component. This
497+
can allow rich components, such as modals and dropdowns. Currently, the only way to pass complex objects to these
498+
children (such as callbacks) is to implement the `DataProvider` interface. (This is how the `Route` component works.)
499+
500+
Example:
501+
502+
```php
503+
#[Component('example')]
504+
readonly class ExampleComponent {
505+
public function render() {
506+
return <<<HTML
507+
<div class='i-have-children'>
508+
<children></children>
509+
</div>
510+
HTML;
511+
}
512+
}
513+
```
514+
515+
# Best Practices
516+
517+
As you may have noticed, the Swytch Framework does not allow you to pass complex objects to child components. This is a
518+
deliberate design decision to keep components pure. If a component has too much logic that depends on the hierarchy of
519+
the HTML, then it becomes difficult to test, maintain, and provide a good developer experience. Instead, we recommend
520+
using stateless components and query caching to improve performance.
521+
522+
For example, if you have a `UserProfile` component that renders a user's profile, you should not pass the user's model
523+
directly to the component, instead, you should pass the user's ID and perform the query in the component's constructor
524+
or in the `render()` method. This allows you to easily rerender the component outside of the tree and easily test the
525+
component.

0 commit comments

Comments
 (0)