From 07bcd2b5ed51feef75d511a83bcb4d7806f7eede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Sun, 25 Mar 2018 04:04:46 +0200 Subject: [PATCH] fix: make sure that onToggle gets called after the update (#20) --- src/index.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index 5bcb82b..bd71e47 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ import {Component} from 'react' import PropTypes from 'prop-types' const callAll = (...fns) => (...args) => fns.forEach(fn => fn && fn(...args)) +const noop = () => {} class Toggle extends Component { static propTypes = { @@ -12,7 +13,7 @@ class Toggle extends Component { } static defaultProps = { defaultOn: false, - onToggle: () => {}, + onToggle: noop, } state = { on: this.getOn({on: this.props.defaultOn}), @@ -69,19 +70,22 @@ class Toggle extends Component { } setOnState = (state = !this.getOn()) => { - if (this.getOn() !== state) { - this.props.onToggle(state, this.getTogglerStateAndHelpers()) - } - this.setState({on: state}) + const cb = + this.getOn() === state + ? noop + : () => { + this.props.onToggle(state, this.getTogglerStateAndHelpers()) + } + this.setState({on: state}, cb) } setOn = this.setOnState.bind(this, true) setOff = this.setOnState.bind(this, false) toggle = this.setOnState.bind(this, undefined) - componentWillReceiveProps(newProps) { - if (newProps.on !== this.props.on && newProps.on !== this.state.on) { - this.setOnState(newProps.on) + componentWillReceiveProps({on}) { + if (on !== this.props.on && on !== this.state.on) { + this.setOnState(on) } }