【问题标题】:react HOC and losing reference to this对 HOC 做出反应并失去对此的参考
【发布时间】:2019-02-18 22:32:28
【问题描述】:

我正在尝试理解 HOC 并将其与 react 和 redux 结合使用。我有以下设置。我将有多个按钮使用 hoc 并传入他们自己的 onClick。并想知道:

一个。这是 HOC 模式的好用法吗?

b.通过在 FooterButton 的渲染函数内部进行此设置,this 引用了 DesignatedWorkerGlobalScope,而 HomeButton 内部的 iconHeigh、iconWidth 和 iconColor 变为未定义或意外值。

任何建议将不胜感激。

import React from 'react';
import { View, Text, TouchableOpacity } from 'react-native';

import { connect } from 'react-redux';
import { compose } from 'redux';

import { getColors, getStylesheet} from "../../styles/StyleManager";

const FooterButtonWrapper = (FooterButtonWrapped, onClick) => { 
    return class FooterButton extends React.Component {

        constructor(props) {
            super(props);

            this.state = {
                Theme: getStylesheet(),
                colors: getColors()
            }            
        }

        _onClick = () => {
            onClick(this.props.dispatch);
        }

        render() {            
            const { Theme, colors } = this.state;

            return(                
                <TouchableOpacity onPress={this._onClick}>
                    <FooterButtonWrapped iconWidth={15} iconHeight={15}  iconColor={"white"}/>            
                </TouchableOpacity>
            )
        }

    }
}

const mapStateToProps = (state, ownProps) => ({});


const composeFooterButton = compose(
    connect(mapStateToProps),
    FooterButtonWrapper 
);

export default composeFooterButton;

然后是一个使用它的按钮:

import React from 'react'; import { View, Text } from 'react-native'; import { push as gotoRoute } from 'react-router-redux'; import { FooterButtonWrapper } from './'; import { Home } from '../../assets'; const HomeButton = ({iconHeight, iconWidth, iconColor}) => ( <View> <Home width={ iconWidth } height={ iconHeight } color={ iconColor }/> <View><Text>Home</Text></View> </View> ); const _onClick = dispatch => { dispatch( gotoRoute( '/' )); } export default FooterButtonWrapper(HomeButton, _onClick);

【问题讨论】:

    标签: reactjs react-native this


    【解决方案1】:

    当你使用它时,它发生了

    const composeFooterButton = compose(
        connect(mapStateToProps),
        FooterButtonWrapper 
    );
    
    export default composeFooterButton;
    

    用于 HOC 函数而不是 Component。

    composeconnect 都可以包装您的组件。 所以你的 HOC 可能是:

    import React from 'react';
    import { View, Text, TouchableOpacity } from 'react-native';
    
    import { connect } from 'react-redux';
    import { compose } from 'redux';
    
    import { getColors, getStylesheet } from '../../styles/StyleManager';
    
    export default function FooterButtonWrapper(FooterButtonWrapped, onClick) {
      class FooterButton extends React.Component {
        constructor(props) {
          super(props);
    
          this.state = {
            Theme: getStylesheet(),
            colors: getColors(),
          };
        }
    
        _onClick = () => {
          onClick(this.props.dispatch);
        };
    
        render() {
          const { Theme, colors } = this.state;
    
          return (
            <TouchableOpacity onPress={this._onClick}>
              <FooterButtonWrapped
                iconWidth={15}
                iconHeight={15}
                iconColor={'white'}
              />
            </TouchableOpacity>
          );
        }
      }
      const mapStateToProps = (state, ownProps) => ({});
    
    
      return connect(mapStateToProps)(FooterButton);  
    }
    

    【讨论】:

    • 谢谢@Nikita!那行得通。那么即使它组合的最后一个函数是连接调用,compose也会返回一个函数?我有点认为这最终会返回一个组件
    • @webhound connect 与 React.Component 一起使用并返回他们自己的 React.Component 实例。 Compose 使用函数:github.com/reduxjs/redux/blob/… 所以你得到了下一个结构:&lt;Connect(T)&gt;&lt; FooterButtonWrapper&gt;&lt;/Connect&gt;,但 FooterButtonWrapper 不应该是一个组件,
    猜你喜欢
    • 2023-04-05
    • 2017-11-08
    • 2021-08-10
    • 1970-01-01
    • 2019-05-19
    • 2018-12-24
    • 2013-10-29
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多