账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react 高阶组件 怎么获取子组件的state?
    27
    0

    http://www.css88.com/react/do...

    看完官网文档,大概知道高阶组件怎么封装了。大概知道高阶组件怎么封装了。

    当前的需求是怎么 获取 WrappedPage 的 state?

    (我们是混合开发,需要更新header和右上角的图标!希望在统一的地方处理header 和右上角功能;而不是每个页面都去添加重复代码。)

    //basic
    import React from 'react';
    import PropTypes from 'prop-types';
    
    
    //modules
    import up from '../modules/cup';
    
    //WrappedPage 每一个jsx路由页面
    export default function mixinsPage(WrappedPage) {
      // ……返回另一个新组件……
      return class Page extends React.Component {
        constructor(props) {
          super(props);
        }
    
        componentWillMount() {
          console.log(WrappedPage);
           //清除页面右上角的图标
           up.plugins.setRightButton();
        }
    
        componentDidMount() {
            console.log(`created=>${this.state.badTitle}`);
            //设置title
            document.title = this.state.badTitle;
            up.plugins.setPageTitle(this.state.badTitle || ' ');//ios 初始化 '' 时 无效
        }
    
        render() {
          // ……使用最新的数据渲染组件
          // 注意此处将已有的props属性传递给原组件
          return <WrappedPage {...this.props} />;
        }
      }
    }
    

    采用 反向继承 模式 :可以操作state 但是会导致WrappedPage的componentWillMount 和 componentWillMount 不执行了。。。。

    export default function mixinsPage(WrappedPage) {
      // ……返回另一个新组件……
      return class Page extends WrappedPage {
        constructor(props) {
          super(props);
        }
    
        componentWillMount() {
          //清除页面右上角的图标
           up.plugins.setRightButton();
        }
    
        componentDidMount() {
       
           //设置title
           document.title = this.state.badTitle;
           up.plugins.setPageTitle(this.state.badTitle || ' ');//ios 初始化 '' 时 无效
        }
    
        shouldComponentUpdate(nextProps, nextState) {
          console.log(nextState);
        }
    
        render() {
          // ……使用最新的数据渲染组件
          // 注意此处将已有的props属性传递给原组件
          // return <WrappedPage {...this.props} />;
          //使用反向继承 模式 :可以操作state
          return super.render()
        }
    
      }
    }

    以前vueJs 的统一处理逻辑!

    //vueJs 的mixins 
    beforeCreate() {
        //清除页面右上角的图标
        plugins.setRightButton();
      },
      created() {
        // log.clearLogs();
    
        console.log(`created=>${this.badTitle}`);
        //设置title
        document.title = this.badTitle;
        plugins.setPageTitle(this.badTitle || ' ');//ios 初始化 '' 时 无效
      },
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 染指流年 普通会员 1楼

      在React中,你可以使用两个关键词来获取子组件的状态:props和state。

      1. props:父组件可以通过props属性来传递子组件的状态。当子组件需要访问到父组件传递过来的状态时,就需要通过props获取。例如:

      ```jsx function ParentComponent() { return (

      ); }

      function ChildComponent(props) { return (

      {props.message}

      ); } ```

      在这个例子中,ChildComponent的state是由ParentComponent传递过来的,所以它可以访问到ParentComponent的message属性。

      1. state:子组件可以通过自己的state属性来管理自己的状态。例如:

      ```jsx function ChildComponent() { return (

      {this.state.message}

      ); }

      function ParentComponent() { return (

      ); } ```

      在这个例子中,ChildComponent有自己的state属性message,所以它可以自己管理自己的状态。

      在React中,无论是在props还是state,都可以使用state属性来获取子组件的状态。但是,如果子组件需要访问到父组件的状态,那么就需要通过props获取。如果子组件不需要访问到父组件的状态,那么就可以通过自己的state来管理自己的状态。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部