关于react.js:你需要的react面试高频考察点总结
Component, Element, Instance 之间有什么区别和分割?元素: 一个元素element是一个一般对象(plain object),形容了对于一个DOM节点或者其余组件component,你想让它在屏幕上出现成什么样子。元素element能够在它的属性props中蕴含其余元素(译注:用于造成元素树)。创立一个React元素element老本很低。元素element创立之后是不可变的。组件: 一个组件component能够通过多种形式申明。能够是带有一个render()办法的类,简略点也能够定义为一个函数。这两种状况下,它都把属性props作为输出,把返回的一棵元素树作为输入。实例: 一个实例instance是你在所写的组件类component class中应用关键字this所指向的货色(译注:组件实例)。它用来存储本地状态和响应生命周期事件很有用。函数式组件(Functional component)基本没有实例instance。类组件(Class component)有实例instance,然而永远也不须要间接创立一个组件的实例,因为React帮咱们做了这些。 connect原理首先connect之所以会胜利,是因为Provider组件:在原利用组件上包裹一层,使原来整个利用成为Provider的子组件 接管Redux的store作为props,通过context对象传递给子孙组件上的connectconnect做了些什么。它真正连贯 Redux 和 React,它包在咱们的容器组件的外一层,它接管下面 Provider 提供的 store 外面的state 和 dispatch,传给一个构造函数,返回一个对象,以属性模式传给咱们的容器组件connect是一个高阶函数,首先传入mapStateToProps、mapDispatchToProps,而后返回一个生产Component的函数(wrapWithConnect),而后再将真正的Component作为参数传入wrapWithConnect,这样就生产出一个通过包裹的Connect组件,该组件具备如下特点 通过props.store获取先人Component的store props包含stateProps、dispatchProps、parentProps,合并在一起失去nextState,作为props传给真正的Component componentDidMount时,增加事件this.store.subscribe(this.handleChange),实现页面交互shouldComponentUpdate时判断是否有防止进行渲染,晋升页面性能,并失去nextState componentWillUnmount时移除注册的事件this.handleChange因为connect的源码过长,咱们只看次要逻辑export default function connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {}) { return function wrapWithConnect(WrappedComponent) { class Connect extends Component { constructor(props, context) { // 从先人Component处取得store this.store = props.store || context.store this.stateProps = computeStateProps(this.store, props) this.dispatchProps = computeDispatchProps(this.store, props) this.state = { storeState: null } // 对stateProps、dispatchProps、parentProps进行合并 this.updateState() } shouldComponentUpdate(nextProps, nextState) { // 进行判断,当数据产生扭转时,Component从新渲染 if (propsChanged || mapStateProducedChange || dispatchPropsChanged) { this.updateState(nextProps) return true } } componentDidMount() { // 扭转Component的state this.store.subscribe(() = { this.setState({ storeState: this.store.getState() }) }) } render() { // 生成包裹组件Connect return ( <WrappedComponent {...this.nextState} /> ) } } Connect.contextTypes = { store: storeShape } return Connect; } }React-Router怎么设置重定向?应用<Redirect>组件实现路由的重定向: ...