goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 323 forks source link

listeners are fired when I write to this.state in Store #620

Closed stukennedy closed 8 years ago

stukennedy commented 8 years ago

I'm wanting to silently set state on the store without triggering listeners.

An example of my Store

import alt from './alt'
import AppActions from './AppActions'

class AppStore {
  constructor () {
    this.bindActions(AppActions)
    this.state = {
      forms: {}
    }
  }

  initForms (data) {
    this.state.forms = data
  }
}
export default alt.createStore(AppStore, 'AppStore')

My Component

import React from 'react'

import AppStore from '../AppStore'
import AppActions from '../AppActions'

let { Component } = React

export default class Input extends Component {
  componentDidMount () {
    AppStore.listen(data => {
       console.log('forms changed', data.forms)
    })
    AppActions.initForms({foo: "bar", bar: "foo"})
  }

  render () {
    return <div>Hello</div>
  }
}

Why is my listener being triggered when I use this.state on the Store, I thought you needed to use this.setState() to trigger the listeners

jdlehman commented 8 years ago

If you return false from your store methods like initForms, it will not trigger the listeners. It's here in the docs.

stukennedy commented 8 years ago

thanks so much, completely missed that