* A descriptor of Component creation, behavior, and lifecycle. * * Deku's primary interface is operated via specification of components. * Components are collections of behavior and state parameterized by immutable, * static properties. A value of type Spec specifies a component o
| 146 | * type Component<P, S> with properties in type P and local state in type S. |
| 147 | */ |
| 148 | interface Spec<P extends HasChildren, S> { |
| 149 | |
| 150 | /** Define a name for the component that can be used in debugging */ |
| 151 | name?: string; |
| 152 | |
| 153 | /** Validate the props sent to your component */ |
| 154 | propTypes?: { [prop: string]: PropSpec }; |
| 155 | |
| 156 | /** |
| 157 | * Render a component. We need to pass in setState so that callbacks on |
| 158 | * sub-components. This may change in the future. |
| 159 | * |
| 160 | * Client: Yes |
| 161 | * Server: Yes |
| 162 | */ |
| 163 | render: (component: Component<P, S>, setState: (newState: S) => void) => VirtualNode<P, S>; |
| 164 | |
| 165 | /** |
| 166 | * Get the initial state for the component. We don't pass props in here like |
| 167 | * React does because the state should just be computed in the render function. |
| 168 | */ |
| 169 | initialState?: () => S; |
| 170 | |
| 171 | /** Default props can be defined that will be used across all instances. */ |
| 172 | defaultProps?: P; |
| 173 | |
| 174 | /** This is called on both the server and the client. */ |
| 175 | beforeMount?: (component: Component<P, S>) => any; |
| 176 | |
| 177 | /** |
| 178 | * This is called after the component is rendered the first time and is only |
| 179 | * ever called once. |
| 180 | * |
| 181 | * Use cases: |
| 182 | * - Analytics tracking |
| 183 | * - Loading initial data |
| 184 | * - Setting the state that should change immediately eg. open/close |
| 185 | * - Adding DOM event listeners on the window/document |
| 186 | * - Moving the element in the DOM. eg. to the root for dialogs |
| 187 | * - Focusing the element |
| 188 | * |
| 189 | * Client: Yes |
| 190 | * Server: No |
| 191 | */ |
| 192 | afterMount?: (component: Component<P, S>, el: Node, setState: (newState: S) => void) => any; |
| 193 | |
| 194 | /** |
| 195 | * This is called once just before the element is removed. It should be used |
| 196 | * to clean up after the component. |
| 197 | * |
| 198 | * Use cases: |
| 199 | * - Unbind window/document event handlers |
| 200 | * - Edit the DOM in anyway to clean up after the component |
| 201 | * - Unbind any event emitters |
| 202 | * - Disconnect streams |
| 203 | * |
| 204 | * Client: Yes |
| 205 | * Server: No |
nothing calls this directly
no outgoing calls
no test coverage detected