I have a small responsive app that I'm playing with, just go to the library. An application is just a series of lists that are populated from the server. When a list item is clicked, the value of this item is added to the list filters at the application level, which will then be used to call up new data to populate the lists.
The problem is that I can not get my lists to be consistent with new data from the application (parent), even when called setState. Here is my code (coffee):
{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 . ?