React Flashcards
React component must act like pure function with respect to their props
React component must act like pure function with respect to their props
Js react callback binding
If you refer to a method without () after it, you should bind that method. In constructor: this.handleClick = this.handleClick.bind(this); Or: use class field syntax. Check React Doc main concepts, 6 onClick ={{ () => this.handleClick()}}
空格还是逗号
tag里面,各等式之间空格,object里,各:pair之间用逗号
&&
true ‘&&’ expression evaluates to expression
false && expression evaluates to false
? :
condition? true: false
Key
Kay is a special string attribute you need to include when creating lists of elements
<li>{...}</li>
Most often, you should use id for key
Not recommended to use index
Key more
Key应该用在构建一组list中,而不是单个li组件。通常在map()call中
Object.keys()
???
return array with keys
Form, controlled components
用setState来控制form的value,开始,this.state规定了value,如果没有包在onChange中的setState来改变value的话,用户是无法输入或更改信息的。而onChange保证了信息的实时更新,不仅仅是UI中的变化,也包括实时的this.state.value
handleChange(event) {
this.setState({value: event.target.value});
}
onChange={this.handleChange}
controlled components, onSubmit preventDefault()
html 自带的,submit以后要refresh浏览器,更新state并把数据传送到后端,React controlled components 自动完成这些,不再需要refresh,而且会造成错误,所以在submit的function里要加入event.preventDefault()
parseInt; parseFloat(string)
User输入的都是string,用这些转换
Object后面用. 还是[]
[‘key’]
.key
props and state
props read only;
state could be modified
Controlled components for multiple inputs
Check class material or react doc
map()经验
map =>后面如果是跟html element,一定不能加{}