更好的ES6方式在React中更新狀態
1 回答

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
解決方法
可以參照這個代碼試一試。
const { quotes } = require('./quotes.json')
classQuoteScreen extends Component {
state = {
QuoteIndex: 0
}
handleUpdateState = () => {
this.setState(({ QuoteIndex }) => ({
QuoteIndex: (QuoteIndex + 1) % (quotes.length - 1)
}));
}
render() {
return (
<Image ...>
<View ...>
...
<ButtonNextQuote
onPress={this.handleUpdateState}
/>
</View>
</Image>
)
}
}
- 1 回答
- 0 關注
- 665 瀏覽
添加回答
舉報
0/150
提交
取消