Skip to content

Commit

Permalink
Add resetState to State
Browse files Browse the repository at this point in the history
  • Loading branch information
istarkov committed Aug 10, 2018
1 parent 8c25957 commit 0c436b4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
18 changes: 9 additions & 9 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"dist/react-powerplug.umd.js": {
"bundled": 22187,
"minified": 8780,
"gzipped": 2415
"bundled": 22402,
"minified": 8890,
"gzipped": 2436
},
"dist/react-powerplug.cjs.js": {
"bundled": 20000,
"minified": 10012,
"gzipped": 2407
"bundled": 20201,
"minified": 10132,
"gzipped": 2430
},
"dist/react-powerplug.esm.js": {
"bundled": 19370,
"minified": 9474,
"gzipped": 2267,
"bundled": 19571,
"minified": 9594,
"gzipped": 2288,
"treeshaked": {
"rollup": {
"code": 204,
Expand Down
8 changes: 6 additions & 2 deletions docs/components/State.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ The onChange event of the State is called whenever the state changes.

## State Children Props

TL;DR: `{ state, setState }`
TL;DR: `{ state, setState, resetState }`

**state**
`object`
Your state

**setState**
`(object | (state: object) => object) => void`
`(object | (state: object) => object, cb?: () => void) => void`
State setter. Is the [setState](https://facebook.github.io/react/docs/react-component.html#setstate) from React.Component.

**resetState**
`(cb?: () => void) => void`
Resets state to initial value (_the value equal to current! initial prop_).
5 changes: 5 additions & 0 deletions src/components/State.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ class State extends Component {
})
}

_resetState = (cb = noop) => {
this._setState({ ...this.props.initial }, cb)
}

render() {
return renderProps(this.props, {
state: this.state,
setState: this._setState,
resetState: this._resetState,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type StateChange<T> = T => void
type StateRender<T> = ({|
state: T,
setState: (updated: $Shape<T> | (T => $Shape<T>), cb?: () => void) => void,
resetState: (cb?: () => void) => void,
|}) => React.Node

type StateProps<T> =
Expand Down
27 changes: 26 additions & 1 deletion tests/components/State.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import { lastCallArg } from './utils'

test('<State />', () => {
const renderFn = jest.fn().mockReturnValue(null)
TestRenderer.create(<State initial={{ myValue: 1 }} render={renderFn} />)

const testRenderer = TestRenderer.create(
<State initial={{ myValue: 1 }} render={renderFn} />
)

// Initial values
expect(renderFn).lastCalledWith({
state: { myValue: 1 },
setState: expect.any(Function),
resetState: expect.any(Function),
})

lastCallArg(renderFn).setState({ myValue: 2 })
Expand All @@ -19,6 +23,27 @@ test('<State />', () => {
expect(renderFn).lastCalledWith({
state: { myValue: 2 },
setState: expect.any(Function),
resetState: expect.any(Function),
})

// Change initial, and update the whole tree
testRenderer.update(<State initial={{ myValue: 3 }} render={renderFn} />)

// Value hasn't been changed
expect(renderFn).lastCalledWith({
state: { myValue: 2 },
setState: expect.any(Function),
resetState: expect.any(Function),
})

// Reset state
lastCallArg(renderFn).resetState()

// Now state value is equal to initial
expect(renderFn).lastCalledWith({
state: { myValue: 3 },
setState: expect.any(Function),
resetState: expect.any(Function),
})
})

Expand Down
8 changes: 7 additions & 1 deletion tests/test_flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ const noop = () => null

/* State with inferred generic */
{
const render = ({ state, setState }) => {
const render = ({ state, setState, resetState }) => {
;(state.v: number)
setState({})
setState({ v: 1 })
Expand All @@ -431,6 +431,12 @@ const noop = () => null
setState({ t: 1 })
// $FlowFixMe
setState({ n: 2 })

resetState()
resetState(() => {})

// $FlowFixMe
resetState(1)
}
const onChange = state => {
;(state.v: number)
Expand Down

0 comments on commit 0c436b4

Please sign in to comment.