父组件经过props向子组件传递参数javascript
父组件将一个函数做为props传递给子组件,子组件调用该回调函数,向父组件通讯。css
context是全局容器,静态属性,使用条件:html
若组件中使用constructor函数,务必在构造函数中传入第二个参数context,并在super调用父类构造函数时也传入context,不然会形成组件中没法使用contextjava
constructor(props,context){ super(props,context); }
Throttle,阻止函数在给定时间内被屡次调用。react
import throttle from 'lodash.throttle'; class LoadMoreButton extends React.Component { constructor(props) { super(props); this.handleClick = this.handleClick.bind(this); this.handleClickThrottled = throttle(this.handleClick, 1000); } componentWillUnmount() { this.handleClickThrottled.cancel(); } render() { return <button onClick={this.handleClickThrottled}>Load More</button>; } handleClick() { this.props.loadMore(); } }
Debounce,确保函数上次执行后的一段时间内,不会再次执行。函数
import debounce from 'lodash.debounce'; class Searchbox extends React.Component { constructor(props) { super(props); this.handleChange = this.handleChange.bind(this); this.emitChangeDebounced = debounce(this.emitChange, 250); } componentWillUnmount() { this.emitChangeDebounced.cancel(); } render() { return ( <input type="text" onChange={this.handleChange} placeholder="Search..." defaultValue={this.props.value} /> ); } handleChange(e) { // React pools events, so we read the value before debounce. // Alternately we could call `event.persist()` and pass the entire event. // For more info see reactjs.org/docs/events.html#event-pooling this.emitChangeDebounced(e.target.value); } emitChange(value) { this.props.onChange(value); } }
参考this