Hooks
Sometimes you need to setup the environment in a specific state, so that the component will rendered correctly. E.g. you have a store object for a user, a session-cookie or you want to check if a value is read from the localStorage.
Gardenjs provides hook-functions which will be executed before or after rendering and after unmounting a component.
location | hook | executed |
---|---|---|
*.das.hook.js | beforeAll | executed before any component inside the directory or below is rendered. When selecting another example or another component in same hierarchy, this function will not be executed again. |
*.das.hook.js | before | executed always when component a new component or example is selected |
*.das.hook.js | after | executed after component is umounted/destroyed |
*.das.hook.js | afterAll | executed after component is unmounted/destroyed and another component outside of the hierarchy is selected |
component description | beforeAll | executed before component is rendered. When selecting another example, this function will not be executed again. |
component description | before | executed always when another example of same component is selected |
component description | after | executed after component is umounted/destroyed |
component description | afterAll | executed after component is unmounted/destroyed and another component is selected |
in example description | before | executed when the example is selected after component was updated |
in example description | after | executed when another example or component is selected |
If you want to share the same setup for all examples of one component use the hooks inside the component description
If you want to share the same setup for all components or a group of components then use hook files ( *.das.hook.js
or *.das.hook.ts
)
Executing order
The order of executing the hooks is as:
- beforeAll hooks
- before hooks
And the order inside beforeAll hooks is
- hook file
- component description
- then example description.
The after hooks are executed in reverse order: first after hooks then afterAll hooks.
Example
Given the following projectstructur:
folder A
glob1.das.hook.js
comp1.das.js
comp2.das.js
folder B
glob2.das.hook.js
comp3.das.js
glob1.das.hook.js
and glob2.das.hook
:
export default {
...
beforeAll: glob1BeforeAll
afterAll: glob1AfterAll
before: glob1Before
after: glob1After
...
}
comp1.das.js
and comp2.das.js
export default {
...
beforeAll: comp1BeforeAll
afterAll: comp1AfterAll
before: comp1Before
after: comp1After
...
examples: [
...
{title: ..., before: example1Before, after: example1After}
{title: ..., before: example2Before, after: example2After}
...
]
}
Selecting a component first
- glob1BeforeAll
- comp1BeforeAll
- glob1Before
- comp1Before
- example1Before
Selecting another example of same component
- example1After
- comp1After
- glob1After
- glob1Before
- comp1Before
- example2Before
Selecting another component in same hierarchy
- example2After
- comp1After
- glob1After
- comp1AfterAll
- comp2BeforeAll
- glob1Before
- comp2Before
- example1Before
Selecting another component in another hierarchy
- example1After
- comp2After
- glob1After
- comp1AfterAll
- glob1AfterAll
- glob2BeforeAll
- comp3BeforeAll
- glob2Before
- comp3Before
- exampleBefore