diff --git a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js index c3c8cf7904e2b..1e185978321c8 100644 --- a/src/renderers/shared/fiber/ReactFiberUpdateQueue.js +++ b/src/renderers/shared/fiber/ReactFiberUpdateQueue.js @@ -76,7 +76,11 @@ exports.callCallbacks = function(queue : UpdateQueue, context : any) { if (callback && !node.callbackWasCalled) { node.callbackWasCalled = true; if (typeof context !== 'undefined') { - callback.call(context); + try { + callback.call(context); + } catch (error) { + // do something + } } else { callback(); } diff --git a/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js b/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js index 5df5c10caf800..7f207b1e9cee0 100644 --- a/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js +++ b/src/renderers/shared/fiber/__tests__/ReactIncremental-test.js @@ -1465,4 +1465,46 @@ describe('ReactIncremental', () => { ]); expect(instance.state.n).toEqual(4); }); -}); + + it('can handle if setState callback throws', () => { + var ops = []; + var instance; + + class Foo extends React.Component { + state = { n: 0 }; + render() { + instance = this; + return
; + } + } + + ReactNoop.render(); + ReactNoop.flush(); + ops = []; + + ReactNoop.syncUpdates(() => { + ReactNoop.batchedUpdates(() => { + instance.setState({ n: 1 }, () => { throw new Error('Bail!') }); + instance.setState({ n: 2 }, () => ops.push('setState 2')); + ReactNoop.batchedUpdates(() => { + instance.setState({ n: 3 }, () => ops.push('setState 3')); + instance.setState({ n: 4 }, () => { throw new Error('Bail Again!') }); + ops.push('end inner batchedUpdates'); + }); + ops.push('end outer batchedUpdates'); + }); + ops.push('end syncUpdates'); + }); + + // ReactNoop.flush() not needed because updates are synchronous + + expect(ops).toEqual([ + 'end inner batchedUpdates', + 'end outer batchedUpdates', + 'setState 2', + 'setState 3', + 'end syncUpdates', + ]); + expect(instance.state.n).toEqual(4); + }); +}); \ No newline at end of file