213
213
214
214
215
215
216
+ < a href ="components.html#lifecycle-hooks " target > < li > Lifecycle hooks</ li > </ a >
217
+
218
+
219
+
220
+
221
+
216
222
< a href ="components.html#component " target > < li > Component</ li > </ a >
217
223
218
224
@@ -294,32 +300,36 @@ <h1>
294
300
295
301
< hr > </ hr >
296
302
297
- < section id ="content "> < h1 id ="components "> Components</ h1 >
298
- < p > GitHub repository < a href ="https://github.com/tbranyen/diffhtml/tree/master/packages/diffhtml-components "> < i class ="fa fa-github "> </ i > </ a > </ p >
299
- < pre > < code class ="language-sh "> npm install diffhtml-components</ code > </ pre >
300
- < ul >
301
- < li > diffHTML does not require components</ li >
302
- < li > Supports a loose interpretation of the React component model and web components</ li >
303
- < li > Components are either stateless (cannot re-render, vs stateful and can re-render)</ li >
304
- < li > </ li >
305
- </ ul >
306
- < p > While the diffHTML API allows you to build large applications and powerful UIs
307
- on its own, if you're coming from another framework you may be looking for a
308
- more structured component-based approach.</ p >
309
- < p > While it's easy to build powerful UIs with the diffHTML API alone, if </ p >
310
- < p > While you can build many great things with the diffHTML API alone, an improved
311
- way of structuring your creations is through the notion of a Component. The
312
- Component will consolidate a section of your UI and make it re-usable and
313
- encapsulated. Over the years many different "component" interfaces have been
314
- created, but the most popular, which diffHTML is based off of, is React's.</ p >
303
+ < section id ="content "> < h1 id ="components-a-classgithub-hrefhttpsgithubcomtbranyendiffhtmltreemasterpackagesdiffhtml-componentsi-classfa-fa-githubia "> Components < a class ="github " href ="https://github.com/tbranyen/diffhtml/tree/master/packages/diffhtml-components "> < i class ="fa fa-github "> </ i > </ a > </ h1 >
304
+ < p > When working on larger and more structured projects, you may find that you want
305
+ to structure your code using a component paradigm like you would find using
306
+ React. This module provides just that, specifically constructors and middleware
307
+ necessary to render a React-like JavaScript class/function or Web Component.</ p >
315
308
< p > < a name ="overview "> </ a > </ p >
316
309
< hr > </ hr >
317
310
< h2 id ="a-hrefoverviewoverviewa "> < a href ="#overview "> Overview</ a > </ h2 >
318
- < p > This package contains middleware to render components in diffHTML. A nice
319
- feature is that you can use any class or function to render a component. So
320
- long as the function or a render method on the class returns a string, VTree,
321
- or DOM Node. The function and class used in this way do not have state and
322
- cannot influence how they re-render.</ p >
311
+ < p > In order to use components, you must install/fetch the components package. This
312
+ contains middleware which will render functions, stateless classes, and
313
+ stateful classes. Unlike middleware, this package will automatically hook its
314
+ middleware into the running diffHTML build.</ p >
315
+ < p > Using components in diffHTML always requires installing or including the base
316
+ package:</ p >
317
+ < pre > < code class ="language-sh "> npm install diffhtml-components</ code > </ pre >
318
+ < p > If you need to import React components, install the compat package:</ p >
319
+ < pre > < code class ="language-sh "> npm install diffhtml-react-compat</ code > </ pre >
320
+ < blockquote >
321
+ < p > This will allow you to replace < code > react</ code > and < code > react-dom</ code > in your project and
322
+ import</ p >
323
+ </ blockquote >
324
+ < p > There are two primary packages for components:
325
+ < a href ="#overview " title ="null "> diffhtml-components</ a >
326
+
327
+ and
328
+ < a href ="#react-compat " title ="null "> diffhtml-react-compat</ a >
329
+ .Unlike other frameworks diffHTML is
330
+ extremely flexible with what you can return, and allows for seamless JSX
331
+ integration, top-level fragments, and supports stateless classes that do not
332
+ need to extend from a base class.</ p >
323
333
< h3 id ="function-component "> Function component</ h3 >
324
334
< pre > < code class ="language-javascript "> import { html, innerHTML } from 'diffhtml';
325
335
@@ -342,19 +352,81 @@ <h3 id="class-component">Class component</h3>
342
352
}
343
353
344
354
innerHTML(document.body, html`<${MyComponent} someProp="value" />`);</ code > </ pre >
355
+ < p > < a name ="lifecycle-hooks "> </ a > </ p >
356
+ < hr > </ hr >
357
+ < h2 id ="a-hreflifecycle-hookslifecycle-hooksa "> < a href ="#lifecycle-hooks "> Lifecycle hooks</ a > </ h2 >
358
+ < p > The following hooks will be called during the respective mounting and
359
+ unmounting flow. You do not need to extend from < code > Component</ code > to use these hooks.
360
+ Simple classes can just define them as methods and they will be called.</ p >
361
+ < h3 id ="hooks "> Hooks</ h3 >
362
+ < ul >
363
+ < li > < code > componentWillMount</ code > </ li >
364
+ < li > < code > componentDidUpdate</ code > </ li >
365
+ < li > < code > componentWillReceiveProps</ code > </ li >
366
+ < li > < code > shouldComponentUpdate</ code > </ li >
367
+ < li > < code > componentWillUnmount</ code > </ li >
368
+ </ ul >
345
369
< p > < a name ="component "> </ a > </ p >
346
370
< hr > </ hr >
347
371
< h2 id ="a-hrefcomponentcomponenta "> < a href ="#component "> Component</ a > </ h2 >
372
+ < p > The stateful class component, which is used by importing the < code > Component</ code > class.</ p >
373
+ < pre > < code class ="language-js "> import { Component } from 'diffhtml-components';</ code > </ pre >
374
+ < p > Once you have this base class, you can extend your ES6 class to get access to
375
+ sub-tree rendering. A component render result is treated as a fragment, and are
376
+ compared with previous results. All top-level elements are tracked and
377
+ efficient diffing is applied against the previously rendered contents. </ p >
378
+ < pre > < code class ="language-js "> import { innerHTML, html } from 'diffhtml-components';
379
+ import { Component } from 'diffhtml-components';
380
+
381
+ class ListComponent extends Component {
382
+ render({ items }) {
383
+ return html`
384
+ ${items.map(item => html`
385
+ <li>${item}</li>
386
+ `)}
387
+ `;
388
+ }
389
+ }
390
+
391
+ innerHTML(document.body, html`
392
+ <ul>
393
+ <${ListComponent} items=${['List item 1', 'List item 2']} />
394
+ </ul>
395
+ `);</ code > </ pre >
396
+ < h3 id ="props "> Props</ h3 >
397
+ < p > These are incoming values that map to the props you set using the element
398
+ attributes. Like in React, there will be a < code > children</ code > prop automatically added
399
+ which maps to the passed in child elements. You can access props on
400
+ < code > this.props</ code > or in the < code > render(props) {}</ code > method.</ p >
401
+ < h3 id ="proptypes "> PropTypes</ h3 >
402
+ < h3 id ="state "> State</ h3 >
403
+ < h4 id ="forceupdate "> forceUpdate</ h4 >
404
+ < h4 id ="setstate "> setState</ h4 >
348
405
< p > < a name ="web-component "> </ a > </ p >
349
406
< hr > </ hr >
350
407
< h2 id ="a-hrefweb-componentweb-componenta "> < a href ="#web-component "> Web Component</ a > </ h2 >
408
+ < p > The WebComponent implementation is nearly identical to the standard
409
+
410
+ < a href ="#component " title ="null "> Component</ a >
411
+ , except < code > this</ code > points to an HTML element, children are
412
+ rendered into the Shadow DOM, and when props and attributes are set on an
413
+ element that match < code > propTypes</ code > cause an automatic re-render.</ p >
414
+ < pre > < code class ="language-js "> import { WebComponent } from 'diffhtml-components';</ code > </ pre >
351
415
< p > < a name ="jsx "> </ a > </ p >
352
416
< hr > </ hr >
353
417
< h2 id ="a-hrefjsxjsxa "> < a href ="#jsx "> JSX</ a > </ h2 >
354
- < p > < a name ="react-compatibility "> </ a > </ p >
355
- < hr > </ hr >
356
- < h2 id ="a-hrefreact-compatibilityreact-compatibilitya "> < a href ="#react-compatibility "> React Compatibility</ a > </ h2 >
418
+ < p > JSX is supported out-of-the-box. </ p >
419
+ < p > < a name ="react-compat "> </ a > </ p >
357
420
< hr > </ hr >
421
+ < h2 id ="a-hrefreact-compatreact-compata "> < a href ="#react-compat "> React Compat</ a > </ h2 >
422
+ < p > This experimental module aims to replicate the public API surface-area of React
423
+ that is sufficient to execute and render components from that ecosystem. The
424
+ reason this is important, is that diffHTML should be able to adopt and help
425
+ contribute back to the community that it borrows so much from. It also helps
426
+ keep down the level of fragmentation required and initial investment if someone
427
+ wants to try out diffHTML in an existing React project.</ p >
428
+ < p > You can install it by running:</ p >
429
+ < pre > < code class ="language-sh "> npm install diffhtml-react-compat</ code > </ pre >
358
430
</ section >
359
431
360
432
< a href ="https://github.com/tbranyen/diffhtml/edit/master/packages/diffhtml-website/pages/components.md " id ="edit-on-github ">
0 commit comments