React JS:调用setState后组件不更新

时间:2014-03-14 13:20:48

标签: coffeescript reactjs

我有一个小小的反应应用程序我正在玩,只是去了解图书馆。该应用程序只是一系列列表,从服务器填充。单击列表项时,该项的值将添加到应用级别的列表过滤器中,然后将用于调用新数据以填充列表。

问题在于,即使在调用setState时,我似乎无法让我的列表与应用(父级)中的新数据进行协调。这是我的代码(咖啡):

### 
@jsx React.DOM
###

{div, h1, h2, h4, ul, li, form, input, br, p, strong, span, a} = React.DOM

SearchApp = React.createClass
  handleTopItemClick: (filter) ->
    facet = filter.field
    filters = @state.filters
    if filters.facets[facet] and filters.facets[facet].length > 0
      filters.facets[facet].push filter.value
    else 
      filters.facets[facet] = [filter.value]

    strArr = []
    _.each filters.facets, (valArr, field) ->
      _.each valArr, (val) ->
        strArr.push "+(#{field}:\"#{val}\")" 

    @setState 
      filters: filters
      queryStr: strArr.join(' ').trim()

  getInitialState: ->
    filters:
      facets: {}
    queryStr: ''    

  render: ->
    (div {
      id: 'content'
      className: "search-wrap"
      },
      (h1 {}, "Search")
      (div
        id: 'widgets',
        (TopList
          title: 'Top Domains'
          params:
            query: @state.queryStr
            field: 'domain'
          onItemClick: @handleTopItemClick
        )
        (TopList
          title: 'Top Senders'
          params:
            query: @state.queryStr
            field: 'from'
          onItemClick: @handleTopItemClick
        )
        (TopList
          title: 'Top Recipient'
          params:
            query: @state.queryStr
            field: 'recipient'
          onItemClick: @handleTopItemClick
        ) 
      )        
    )

TopItem = React.createClass
  getDefaultProps: ->
    value: ''
    count: 0
    field: null

  render: ->
    (li {},
      (a {
        onClick: @handleClick
        className: 'top-item-filter'
        title: @props.value
        },
        (strong {}, @props.value)
        (span {}, @props.count)
      )
    )

  handleClick: (event) ->
    event.preventDefault()
    @props.onItemClick @props.value

TopList = React.createClass
  getInitialState: ->
    data: []
    params: @props.params

  componentWillReceiveProps: (nextProps) ->
    @setState params: nextProps.params

  componentWillMount: ->
    request.post("/facet").send(@state.params).end (results) =>
      @setState data: JSON.parse(results.text)

  render: ->
    itemNodes = _.map @state.data, (item) =>
      key = item.value
      TopItem
        value: item.value
        count: item.count
        key: key
        onItemClick: @handleItemClick

    (div {className: 'widget top-item'},
      (h2 {className: 'widget-header'}, "#{@props.title}")
      (ul {className: 'top-items-list'}, itemNodes)
    )

  handleItemClick: (value) ->
    @props.onItemClick
      value: value
      field: @props.params.field

React.renderComponent SearchApp(null), document.getElementById("content")

列表第一次渲染正常,按预期获取未过滤的数据。当我单击列表项时,SearchApp会收到该事件,并相应地更新其自己的状态。 componentWillReceiveProps类已正确调用TopList,但setState调用不会更新其状态,因此,它们不会进行协调。我已验证nextProps包含新信息。我错过了什么?

1 个答案:

答案 0 :(得分:5)

没有任何理由将道具保存在州内;并且将道具作为真相的来源,更不容易出错。它还简化了代码。

但对于实际问题; componentWillMount仅在此处调用一次。如果要在传递新参数时重复AJAX请求;你可以这样做:

TopList = React.createClass
  getInitialState: ->
    data: []

  getSearchResultsFromServer: (params) ->
    request.post("/facet").send(params).end (results) =>
      if @isMounted()
        @setState data: JSON.parse(results.text) 

  componentWillReceiveProps: (nextProps) -> @getSearchResultsFromServer nextProps.params
  componentDidMount: -> @getSearchResultsFromServer @props.params

  render: ->
    itemNodes = _.map @state.data, (item) =>
      key = item.value
      TopItem
        value: item.value
        count: item.count
        key: key
        onItemClick: @handleItemClick

    (div {className: 'widget top-item'},
      (h2 {className: 'widget-header'}, "#{@props.title}")
      (ul {className: 'top-items-list'}, itemNodes)
    )

  handleItemClick: (value) ->
    @props.onItemClick
      value: value
      field: @props.params.field

理想情况下,您的ajax库会支持中止请求;在componentWillUnmount中,您可以中止这些请求。