gpt4 book ai didi

coffeescript - 调用 setState 后 React JS : Component not updating,

转载 作者:行者123 更新时间:2023-12-03 13:43:15 27 4
gpt4 key购买 nike

我正在使用一个小型的 React 应用程序,只需去掌握该库的窍门即可。该应用程序只是一系列列表,由服务器填充。单击列表项时,该项目的值将添加到应用级别的列表过滤器中,然后用于调用新数据来填充列表。

问题是,即使在调用 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 会接收该事件,并相应地更新其自身的状态。在 TopList 类上正确调用了 componentWillReceiveProps,但其中的 setState 调用不会更新其状态,因此,它们不会协调。我已验证 nextProps 包含新信息。我错过了什么?

最佳答案

实际上没有任何理由将 Prop 保存在状态中;而且将 props 作为事实来源更不容易出错。它还稍微简化了代码。

对于实际问题,不过; 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 中中止这些请求。

关于coffeescript - 调用 setState 后 React JS : Component not updating,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22406188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com