React 相关问题

1、this.props.history传值

2、react 如何监听路由变化

  • 分2种情况:

1、普通react组件(指不是通过Route导入的组件)普通组件需要自己定义上下文

  1. staticcontextTypes = {
  2. router:PropTypes.shape({
  3. history:PropTypes.shape({
  4. push:PropTypes.func.isRequired,
  5. replace:PropTypes.func.isRequired,
  6. createHref:PropTypes.func.isRequired
  7. }).isRequired
  8. }).isRequired
  9. }
  10. componentDidMount(){
  11. this.context.router.history.listen(()=>{
  12. })
  13. }

2、Route导入的组件Route组件已经封装好了上下文,并且设置了props可用。

  1. componentDidMount(){
  2. this.props.history.listen(()=>{
  3. })
  4. }

原生js的办法是:

  1. window.addEventListener('hashchange',()=>{ })

React 相关库

1、状态管理

ReduxRedux 中文官网Redux 中文文档React Redux

2、异步提交数据中间件:

redux-saga:Redux-Sagaredux-saga 中文网Redux-saga 用法
redux-thunk:redux-thunk

3、路由

React Router

4、React 组件和库列表

awesome-react

5、React 移动端

React Native