Skip to content

Commit

Permalink
initialState > init, contrib #3
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Jan 8, 2018
1 parent 0ebbf05 commit 36c4e25
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 26 deletions.
4 changes: 2 additions & 2 deletions examples/sandbox/app/components/MyComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = connect(state => ({
text: state.text
}))(
component({
initialState (props) {
return {
init (props) {
this.state = {
title: 'Hello'
}
},
Expand Down
4 changes: 1 addition & 3 deletions examples/sandbox/app/pages/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ const h = require('@loll/h')
const MyComponent = require('../components/MyComponent.js')

module.exports = function Home (props) {
const content = MyComponent('Hello')
console.log(content)
return h`
<div>
<h1 css=${props === 'Home' ? {
Expand All @@ -14,7 +12,7 @@ module.exports = function Home (props) {
color: 'palevioletred'
}}>${props}</h1>
${content}
${MyComponent('Hello')}
</div>
`
}
16 changes: 8 additions & 8 deletions packages/loll-component/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Add state:
```javascript
// MyComponent.js
module.exports = component({
initialState (props) {
return {
init (props, state) {
this.state = {
count: 0
}
},
Expand All @@ -44,8 +44,8 @@ Add methods:
```javascript
// MyComponent.js
module.exports = component({
initialState (props) {
return {
init (props, state) {
this.state = {
count: 0
}
},
Expand Down Expand Up @@ -76,8 +76,8 @@ Prevent updating. Think `shouldComponentUpdate`. The below will never update:
```javascript
// MyComponent.js
module.exports = component({
initialState (props) {
return {
init (props, state) {
this.state = {
count: 0
}
},
Expand Down Expand Up @@ -111,8 +111,8 @@ Async actions and incremental rendering:
```javascript
// MyComponent.js
module.exports = component({
initialState (props) {
return {
init (props, state) {
this.state = {
loading: false,
posts: props.posts
}
Expand Down
10 changes: 5 additions & 5 deletions packages/loll-component/dist/loll-component.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/loll-component/dist/loll-component.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/loll-component/dist/loll-component.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions packages/loll-component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ function merge (one, two) {
module.exports = function Component (comp) {
assert.ok(typeof comp === 'object', 'component is not an object')
assert.ok(typeof comp.render === 'function', 'component.render() must be a function')
if (comp.update) {
assert.ok(typeof comp.update === 'function', 'component.update() must be a function')
}
if (comp.update) assert.ok(typeof comp.update === 'function', 'component.update() must be a function')

Object.assign(comp, {
state: {},
Expand Down Expand Up @@ -43,16 +41,18 @@ module.exports = function Component (comp) {
assert.ok(typeof externalState === 'object', 'external state passed to component must be an object')

if (!comp.ref) {
comp.state = Object.assign(
comp.init && comp.init(props, externalState)

comp.state = merge(
externalState,
comp.initialState ? comp.initialState(props) : {}
comp.state
)

comp.ref = comp.render(props, comp.state)
comp.props = props
}

if (!shouldUpdate(props, merge(comp.state, externalState))) {
if (!shouldUpdate(props, merge(externalState, comp.state))) {
return comp.ref
}

Expand Down

0 comments on commit 36c4e25

Please sign in to comment.