• xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!


    React Hooks & react forwardref hooks & useReducer

    react how to call child component method in another child component

    1. left index list => right map
    2. right map back to default value, right child call left child methods ???

    https://stackoverflow.com/a/37950970/5934465

    https://reactjs.org/docs/hooks-reference.html#usereducer

    https://reactjs.org/docs/forwarding-refs.html


    store wrapper bug

    OK

    import React, {
      Component,
      // useState,
      // useEffect,
    } from 'react';
    
    class ChildA extends Component {
      constructor(props){
        super(props);
        this.state = {};
      }
      // clickRefTest = (key = ``) => {
      //   console.log(`ref click key`, key);
      // };
      clickCheck = (key) => {
        console.log(`ref click key`, key);
      };
      render() {
        return (
          <div>
            ChildA Components without @connect store
          </div>
        );
      }
    }
    
    
    // const ChildA = (props) => {
    //   return (
    //     <>
    //       <div>
    //         <button onClick={this.props.refClick}>Click</button>
    //       </div>
    //     </>
    //   );
    // };
    
    export {
      ChildA,
    };
    
    export default ChildA;
    
    
    

    bug

    import React, {
      Component,
      // useState,
      // useEffect,
    } from 'react';
    
    import { bindActionCreators } from 'redux';
    import { connect } from 'dva';
    
    import dashboardRegionActions from '../actions/dashboard.region.action';
    
    import { SelectOption } from "./SelectOption";
    import { SearchInput } from "./SearchInput";
    
    import * as Provinces from "../Region/provinces.json";
    
    import './index.less';
    
    import {
      Row,
      Col,
    } from "antd";
    
    const mapStateToProps = ({ dashboard }) => ({
      regionData: dashboard.regionData,
    });
    
    const mapDispatchToProps = dispatch => ({
      actions: bindActionCreators(dashboardRegionActions, dispatch),
    });
    
    @connect(mapStateToProps, mapDispatchToProps,)
    class SelectableIndexList extends Component {
      constructor(props) {
        super(props);
        this.state = {
          placeholder: "请输入省份名或其拼音",
          provincesList: this.selectedFilter(Provinces.default),
          initProvincesList: this.selectedFilter(Provinces.default),
        };
      }
      selectedFilter = (provinces = []) => {
        return provinces.map(obj => {
          obj.selected = obj.key === "all";
          return obj;
        });
      };
      formatAdcode = (code = "all") => {
        let adcode = 100000;
        if (code !== "all") {
          adcode = Number(`${code}0000`);
        }
        return adcode;
      };
      provincesFilter = (provincesList, key) => {
        const result = provincesList.map(({name, key: code,}) => {
          return {
            name,
            key: code,
            selected: code === key,
          };
        }) || [];
        return result;
      };
      clickRefTest = (key = ``) => {
        console.log(`ref click key`, key);
      };
      clickCheck = (key) => {
        const {
          actions: {
            changeRegion,
          },
          customizeRefCallback,
        } = this.props;
        const {
          provincesList,
          initProvincesList,
        } = this.state;
        const newProvincesList = this.provincesFilter(provincesList, key);
        const newInitProvincesList = this.provincesFilter(initProvincesList, key);
        this.setState({
          provincesList: newProvincesList,
          initProvincesList: newInitProvincesList,
        }, () => {
          changeRegion(`${key}`);
          customizeRefCallback();
        });
      };
      handleCheckableTagChange = (value) => {
        const {
          initProvincesList: provincesList,
        } = this.state;
        const newProvincesList = provincesList.filter(obj => obj.name.includes(value)) || [];
        this.setState({
          provincesList: newProvincesList,
        });
      };
      inputChangeHandle = (e) => {
        const value = e.target.value;
        this.handleCheckableTagChange(value);
      };
      render() {
        const {
          placeholder,
          provincesList,
        } = this.state;
        return (
          <>
            <Row className="selectable-index-list">
              <Col span={24} className="input-box">
                {
                  SearchInput({
                    placeholder: placeholder,
                    inputChangeHandle: this.inputChangeHandle,
                  })
                }
              </Col>
              <Col span={24} className="option-box">
                <SelectOption
                  provincesList={provincesList}
                  clickCheck={this.clickCheck}
                />
              </Col>
            </Row>
          </>
        );
      }
    }
    
    export {
      SelectableIndexList,
    };
    
    export default SelectableIndexList;
    
    
    
    

    solution

    @connect & withRef

    missing null

    OK

    width !== with

    withRef

    
    import React, {
      Component,
      // useState,
      // useEffect,
    } from 'react';
    
    import { bindActionCreators } from 'redux';
    import { connect } from 'dva';
    
    import dashboardRegionActions from '../actions/dashboard.region.action';
    
    import { SelectOption } from "./SelectOption";
    import { SearchInput } from "./SearchInput";
    
    import * as Provinces from "../Region/provinces.json";
    
    import './index.less';
    
    import {
      Row,
      Col,
    } from "antd";
    
    const mapStateToProps = ({ dashboard }) => ({
      regionData: dashboard.regionData,
    });
    
    const mapDispatchToProps = dispatch => ({
      actions: bindActionCreators(dashboardRegionActions, dispatch),
    });
    
    // 第四个参数 ??? withRef
    @connect(mapStateToProps, mapDispatchToProps, null, { withRef: true,})
    class SelectableIndexList extends Component {
      constructor(props) {
        super(props);
        this.state = {
          placeholder: "请输入省份名或其拼音",
          provincesList: this.selectedFilter(Provinces.default),
          initProvincesList: this.selectedFilter(Provinces.default),
        };
      }
      selectedFilter = (provinces = []) => {
        return provinces.map(obj => {
          obj.selected = obj.key === "all";
          return obj;
        });
      };
      formatAdcode = (code = "all") => {
        let adcode = 100000;
        if (code !== "all") {
          adcode = Number(`${code}0000`);
        }
        return adcode;
      };
      provincesFilter = (provincesList, key) => {
        const result = provincesList.map(({name, key: code,}) => {
          return {
            name,
            key: code,
            selected: code === key,
          };
        }) || [];
        return result;
      };
      clickRefTest = (key = ``) => {
        console.log(`ref click key`, key);
      };
      clickCheck = (key) => {
        const {
          actions: {
            changeRegion,
          },
          customizeRefCallback,
        } = this.props;
        const {
          provincesList,
          initProvincesList,
        } = this.state;
        const newProvincesList = this.provincesFilter(provincesList, key);
        const newInitProvincesList = this.provincesFilter(initProvincesList, key);
        this.setState({
          provincesList: newProvincesList,
          initProvincesList: newInitProvincesList,
        }, () => {
          changeRegion(`${key}`);
          customizeRefCallback();
        });
      };
      handleCheckableTagChange = (value) => {
        const {
          initProvincesList: provincesList,
        } = this.state;
        const newProvincesList = provincesList.filter(obj => obj.name.includes(value)) || [];
        this.setState({
          provincesList: newProvincesList,
        });
      };
      inputChangeHandle = (e) => {
        const value = e.target.value;
        this.handleCheckableTagChange(value);
      };
      render() {
        const {
          placeholder,
          provincesList,
        } = this.state;
        return (
          <>
            <Row className="selectable-index-list">
              <Col span={24} className="input-box">
                {
                  SearchInput({
                    placeholder: placeholder,
                    inputChangeHandle: this.inputChangeHandle,
                  })
                }
              </Col>
              <Col span={24} className="option-box">
                <SelectOption
                  provincesList={provincesList}
                  clickCheck={this.clickCheck}
                />
              </Col>
            </Row>
          </>
        );
      }
    }
    
    export {
      SelectableIndexList,
    };
    
    export default SelectableIndexList;
    
    
    
    

    withRef

    https://github.com/reduxjs/react-redux/issues/1053

    forwardRef

    https://stackoverflow.com/questions/53819335/withref-is-removed-to-access-the-wrapped-instance-use-a-ref-on-the-connected-c

    https://medium.com/@mehran.khan/using-refs-with-react-redux-6-how-to-use-refs-on-connected-components-4b80d4ea7300



    ©xgqfrms 2012-2020

    www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


  • 相关阅读:
    大数据第59天—MySQL之员工奖金-杨大伟
    大数据第58天—MySQL常用命令-杨大伟
    大数据第56天—Mysql练习题12道之十一-查出销售表中所有会员购买金额,同时分组查出退货表中所有会员的退货金额-杨大伟
    大数据第55天—Mysql练习题12道之十-查询各自区组的money排名前十的账号-杨大伟
    大数据第54天—Mysql练习题12道之九-充值日志表-杨大伟
    大数据第53天—Mysql练习题12道之八-线上服务器访问日志-杨大伟
    大数据第52天—Mysql练习题12道之七-图书管理数据库-杨大伟
    mac 破解pycharm2020.2.3
    mac连接oracle数据库
    python DPI-1047:Cannot locate a 64-bit Oracle Client library:The specified module could not be found.
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/11753108.html
Copyright © 2020-2023  润新知