Skip to content

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/__tests__/index.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ describe('reactTreeWalker', () => {
101101

102102
componentWillMount() {
103103
this.setState({ foo: 'bar' })
104+
this.setState((state, props) => ({ other: `I am ${props.value} ${state.foo}` }))
104105
}
105106

106107
render() {
@@ -109,8 +110,8 @@ describe('reactTreeWalker', () => {
109110
}
110111
}
111112

112-
return reactTreeWalker(<Baz />, () => true).then(() => {
113-
const expected = { foo: 'bar' }
113+
return reactTreeWalker(<Baz value="foo" />, () => true).then(() => {
114+
const expected = { foo: 'bar', other: 'I am foo bar' }
114115
expect(actual).toMatchObject(expected)
115116
})
116117
})

src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ export default function reactTreeWalker(
136136

137137
// Make the setState synchronous.
138138
instance.setState = newState => {
139+
if (typeof newState === 'function') {
140+
// eslint-disable-next-line no-param-reassign
141+
newState = newState(
142+
instance.state,
143+
instance.props,
144+
instance.context,
145+
)
146+
}
139147
instance.state = Object.assign({}, instance.state, newState)
140148
}
141149

0 commit comments

Comments
 (0)