You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feature/persistent state and Feature/nested-components (#16)
* added a persistent storage add-on:
- called "storage"
- uses localStorage, works in browser only for now
- tested and working with Counter and Todo apps so far
- TODO: add NodeJS support, do more testing, and then code cleanups
* fix/nested-components (#17)
- fix in `html` - register the right event handlers with the right elems:
Create a `html.funcs` as well as a `html.i`, and use those
as references to the functions to be inserted.
This fixes Component so that the new stuff in
examples/recipes.js works OK.
NOTE: `html` will detect a statful Component, and will only
retrieve its view, **not** run its rull setState()/render()
lifecycle. This has a number of implications:
- better performance
- parent component holds the only state
- parent component inly triggers re-renders
- nested stateful components have a undefined `.container`
- therefore calling setState() of child components does nothing
* updated examples
* updated README
Copy file name to clipboardExpand all lines: README.md
+202-2Lines changed: 202 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -36,6 +36,7 @@ A "state" is a snapshot of your application data at a specific time.
36
36
-`validator`: validate states against a schema (like a simple PropTypes)
37
37
-`html`/`htmel`: simpler, more powerful Template Literals (like a simple JSX)
38
38
-`emitter`: an event emitter, for sharing updates between components
39
+
-`storage`: enables persistent states (between page refreshes, etc)
39
40
-`tweenState`: animate from one state to the next
40
41
- Supports **"middleware"** functions:
41
42
- easily customise a components setState and re-render behaviour
@@ -47,7 +48,7 @@ A "state" is a snapshot of your application data at a specific time.
47
48
- a log of all state history can be kept, for debugging (_optional_):
48
49
- rewind or fast-forward to any point in the state history
49
50
- save/load current or any previous state as "snapshots"
50
-
-Simple, stateless "child" components
51
+
-Nested components
51
52
- ...and more
52
53
53
54
@@ -93,7 +94,6 @@ Todo.view = props => htmel
93
94
Todo.render('.container')
94
95
```
95
96
96
-
97
97
### A *re-usable* HTML component:
98
98
99
99
Unlike the previous two examples, this one below is a function which generates re-usable components - a new component is created from the given definition (state, view, etc) each time it's called.
See [examples/usage-persistant-state.js](examples/usage-persistant-state.js) for more info.
655
+
544
656
### Using the `tweenState` module
545
657
546
658
With `tweenState` it's super easy to do animations that use `requestAnimationFrame` and DOM diffing.
@@ -819,6 +931,94 @@ Adding linked data to your components is easy - just define it as part of your v
819
931
- use the `props` passed in to define/update whatever you need
820
932
- your JSON-LD will be updated along with your view, whenever your component re-renders
821
933
934
+
### Nested components
935
+
936
+
Components that are nested inside other components are called _child components_.
937
+
938
+
There are two kinds of child component - _stateless_ and _stateful_ - and while they behave the same in most ways, they have slightly difference syntax and features.
939
+
940
+
All child components have the following in common:
941
+
- you include the child component in the "view" of the parent component
942
+
- child components do not trigger a re-render of the page
943
+
- to re-render a child component that has changed, you must update the parent component
944
+
- nested components work with or without the `html`/`htmel` add-on(s)
945
+
946
+
**About "stateless" child components:**
947
+
948
+
Stateless components are just _regular functions_ that take `props` as input, and return a view - usually HTML as a string.
949
+
950
+
```js
951
+
// a stateless child component is just a function that receives `props`, and returns a view
952
+
consth2=text=>`<h2>${text}</h2>`;
953
+
954
+
// ...used inside the view of another component:
955
+
Foo.view=props=>`
956
+
<div>
957
+
${h2(props.txt)}
958
+
<p> ... </p>
959
+
</div>
960
+
`;
961
+
```
962
+
963
+
**About stateful child components**
964
+
965
+
Stateful components are _any components with a state_, usually created like so:
966
+
967
+
```js
968
+
constFoo=newComponent({ ...someData });
969
+
```
970
+
971
+
NOTE: When nested inside another component, even stateful components _do not_ run `setState()` & `render()` - they simply return their view, just like stateless child components.
972
+
973
+
This has a number of implications:
974
+
975
+
- better performance (fewer page re-renders)
976
+
- enforces similar behaviour to stateless child components
977
+
- only parent components trigger page re-renders
978
+
- nested components have an undefined `.container` property
979
+
- therefore calling the `render()` method of a child component (usually) does nothing
980
+
- calling `setState()` of a child component _will_ update its state and run its "middleware", but _doesn't_ re-render
981
+
982
+
```js
983
+
// let's create a re-usable, stateful button component:
984
+
// the `htmel` add-on is used as we're attaching Event Listeners to the buttons
0 commit comments