Skip to content

Commit

Permalink
fix(AutoControlledComponent): use setState instead of an assign (#1799)
Browse files Browse the repository at this point in the history
* fix(AutoControlledComponent): use setState instead of an assign

* refactor(ACC): rename initialState to getInitialState
  • Loading branch information
layershifter authored and levithomason committed Jun 26, 2017
1 parent 6da73f4 commit dbc3b00
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 26 deletions.
2 changes: 0 additions & 2 deletions src/addons/Portal/Portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ class Portal extends Component {
type: META.TYPES.ADDON,
}

state = {}

componentDidMount() {
debug('componentDidMount()')
this.renderPortal()
Expand Down
9 changes: 6 additions & 3 deletions src/lib/AutoControlledComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ export const getAutoControlledStateValue = (propName, props, state, includeDefau
}

export default class AutoControlledComponent extends Component {
componentWillMount() {
constructor(...args) {
super(...args)

const { autoControlledProps } = this.constructor
const state = _.invoke(this, 'getInitialState', this.props) || {}

if (process.env.NODE_ENV !== 'production') {
const { defaultProps, name, propTypes } = this.constructor
Expand Down Expand Up @@ -130,7 +133,7 @@ export default class AutoControlledComponent extends Component {
// Also look for the default prop for any auto controlled props (foo => defaultFoo)
// so we can set initial values from defaults.
const initialAutoControlledState = autoControlledProps.reduce((acc, prop) => {
acc[prop] = getAutoControlledStateValue(prop, this.props, this.state, true)
acc[prop] = getAutoControlledStateValue(prop, this.props, state, true)

if (process.env.NODE_ENV !== 'production') {
const defaultPropName = getDefaultPropName(prop)
Expand All @@ -146,7 +149,7 @@ export default class AutoControlledComponent extends Component {
return acc
}, {})

this.state = { ...this.state, ...initialAutoControlledState }
this.state = { ...state, ...initialAutoControlledState }
}

componentWillReceiveProps(nextProps) {
Expand Down
9 changes: 2 additions & 7 deletions src/modules/Accordion/Accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,8 @@ export default class Accordion extends Component {
static Content = AccordionContent
static Title = AccordionTitle

state = {}

constructor(...args) {
super(...args)
this.state = {
activeIndex: this.props.exclusive ? -1 : [-1],
}
getInitialState({ exclusive }) {
return { activeIndex: exclusive ? -1 : [-1] }
}

handleTitleClick = (e, index) => {
Expand Down
2 changes: 0 additions & 2 deletions src/modules/Checkbox/Checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ export default class Checkbox extends Component {
type: META.TYPES.MODULE,
}

state = {}

componentDidMount() {
this.setIndeterminate()
}
Expand Down
1 change: 0 additions & 1 deletion src/modules/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ export default class Dropdown extends Component {
static Menu = DropdownMenu

componentWillMount() {
if (super.componentWillMount) super.componentWillMount()
debug('componentWillMount()')
const { open, value } = this.state

Expand Down
2 changes: 0 additions & 2 deletions src/modules/Embed/Embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ export default class Embed extends Component {
type: META.TYPES.MODULE,
}

state = {}

getSrc() {
const {
autoplay = true,
Expand Down
2 changes: 0 additions & 2 deletions src/modules/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ class Modal extends Component {
static Description = ModalDescription
static Actions = ModalActions

state = {}

componentWillUnmount() {
debug('componentWillUnmount()')
this.handlePortalUnmount()
Expand Down
1 change: 0 additions & 1 deletion src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ export default class Search extends Component {
static Results = SearchResults

componentWillMount() {
if (super.componentWillMount) super.componentWillMount()
debug('componentWillMount()')
const { open, value } = this.state

Expand Down
3 changes: 0 additions & 3 deletions src/modules/Sidebar/Sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,8 @@ class Sidebar extends Component {
}

static Pushable = SidebarPushable

static Pusher = SidebarPusher

state = {}

startAnimating = (duration = 500) => {
clearTimeout(this.stopAnimatingTimer)

Expand Down
4 changes: 2 additions & 2 deletions src/modules/Tab/Tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class Tab extends Component {

static Pane = TabPane

state = {
activeIndex: 0,
getInitialState() {
return { activeIndex: 0 }
}

handleItemClick = (e, { index }) => {
Expand Down
4 changes: 3 additions & 1 deletion test/specs/lib/AutoControlledComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ let TestClass
const createTestClass = (options = {}) => class Test extends AutoControlledComponent {
static autoControlledProps = options.autoControlledProps
static defaultProps = options.defaultProps
state = options.state
getInitialState() {
return options.state
}
render = () => <div />
}
/* eslint-enable */
Expand Down

0 comments on commit dbc3b00

Please sign in to comment.