我可以将state属性设置为回调吗?

时间:2019-03-29 14:34:59

标签: javascript reactjs

可以在回调中设置状态吗?该代码实际上有效,但是我需要知道它是否适合设置状态的良好做法。

this.setState(this.props.onSuccess(data), () => {loading: false})

3 个答案:

答案 0 :(得分:3)

您可能想在挂钩本身上设置状态:

onSuccess(data) {
 // ...
 this.setState({loading: false})
}

答案 1 :(得分:2)

//错误

Sheet11.Range("h:p").ClearContents  'clear old data
url = "URL;http://fantasy.espn.com/baseball/league/rosters?leagueId=101823"

With Sheet11.QueryTables.Add(Connection:= _
    url, Destination:=Range("$h$1"))
    .Name = "MyESPNRoster"
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .BackgroundQuery = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = False
    .RefreshPeriod = 0
    .WebSelectionType = xlSpecifiedTables
    .WebTables = "3,4,5,6,7,8,9,10,11,12,13,14"    'the table number to get the right table of data. there should be 12 rosters
    .WebFormatting = xlWebFormattingNone
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = True
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
End With

//右

this.setState({ count: this.state.count + props.count})
doSomething(this.state.count)
  • 计算时不要依赖当前状态

答案 2 :(得分:1)

这会更有效吗?

alpha

我认为您使用的模式根本不是很常见。