下面是一个简单的反击计数器.但我有3个困惑.
第3行的状态是什么?看起来像一个全局变量,它有没有意义,如果它有var或const之前.这是一个生命周期函数/ var吗?
我必须从反应中导入组件吗?我记得我不需要在第15节那样做.
prevState来自哪里?
Run Code Online (Sandbox Code Playgroud)import React, { Component } from 'react'; class Counter extends Component { state = { value: 0 }; increment = () => { this.setState(prevState => ({ value: prevState.value + 1 })); }; decrement = () => { this.setState(prevState => ({ value: prevState.value - 1 })); }; render() { return ( <div> {this.state.value} <button onClick={this.increment}>+</button> <button onClick={this.decrement}>-</button> </div> ) } }
我无法理解为什么在箭头函数中我们不需要在({})大括号中包含箭头函数的文字,而不是在这个示例中只包含在单个()大括号中的文字.为什么?我曾在网上冲浪找到答案,但失败了.
还有为什么我们把参数放在双括号({})中而不仅仅是()?
const FilterLink = ({ filter, children }) => (
<NavLink
to={filter === 'SHOW_ALL' ? '/' : `/${ filter }`}
activeStyle={ {
textDecoration: 'none',
color: 'black'
}}
>
{children}
</NavLink>
)
Run Code Online (Sandbox Code Playgroud)