【问题标题】:Calling a React function PropType from outside the class definition从类定义外部调用 React 函数 PropType
【发布时间】:2016-08-07 21:05:02
【问题描述】:

我有以下 React 组件..

import React, { Component,PropTypes } from 'react';
import RequestListItem from '../RequestListItem';
import { ScrollView,Text,View } from 'react-native';

class  RequestList extends Component {


  render(){
    return (
      <ScrollView
        onScroll={() => { console.log('onScroll!'); }}
        automaticallyAdjustContentInsets={false}
        scrollEventThrottle={200}>
        {this.props.requests.map(mapRequests)}
      </ScrollView>
    );
  }
}

RequestList.propTypes = {
  requests: PropTypes.array.isRequired,
  onRequestItemClick: PropTypes.func.isRequired
};

var mapRequests = (request, i) => {
  <RequestListItem
      id={request.id}
      title={request.title}
      onRequestItemClick={this.props.onRequestItemClick.bind(this)}
  />
};

export default RequestList;

我遇到的问题与mapRequest 函数有关。我需要能够调用作为属性传递给该组件的onRequestItemClick,但由于这是在类定义之外定义的,因此我似乎无法访问这些属性。如何完成上面的代码试图做的事情?

【问题讨论】:

    标签: reactjs react-native redux react-redux


    【解决方案1】:

    您可以将回调传递给mapRequests 方法,而不是尝试直接从道具中拉取它:

    class RequestList extends Component {
      constructor(props) {
          super(props);
    
          this.props.onRequestItemClick = this.props.onRequestItemClick.bind(this) // I'm not sure why your binding to this something you got from the props, so consider removing this line
      }
    
      render(){
        return (
          <ScrollView
            onScroll={() => { console.log('onScroll!'); }}
            automaticallyAdjustContentInsets={false}
            scrollEventThrottle={200}>
            {this.props.requests.map((request) => mapRequests(request, this.props.onRequestItemClick))} // the arrow function calls mapRequest, and passes the request and the callback 
          </ScrollView>
        );
      }
    }
    
    var mapRequests = (request, onRequestItemClick) => {
      <RequestListItem
          id={request.id}
          title={request.title}
          onRequestItemClick={onRequestItemClick}
      />
    };
    

    但是,如果您已经使用匿名函数,则无论如何都不需要mapRequests 函数:

    class RequestList extends Component {
      constructor(props) {
          super(props);
    
          this.props.onRequestItemClick = this.props.onRequestItemClick.bind(this) // I'm not sure why your binding to this something you got from the props, so consider removing this line
      }
    
      render(){
        return (
          <ScrollView
            onScroll={() => { console.log('onScroll!'); }}
            automaticallyAdjustContentInsets={false}
            scrollEventThrottle={200}>
            {this.props.requests.map((request) => (
                <RequestListItem
                    id={request.id}
                    title={request.title}
                    onRequestItemClick={this.props.onRequestItemClick}        
                />
            )}
          </ScrollView>
        );
      }
    }
    

    【讨论】:

    • 谢谢!我最初尝试过,但遇到了问题,但我回去再试一次,它成功了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 2015-07-19
    • 2017-01-21
    • 2019-09-17
    • 1970-01-01
    • 2011-02-18
    • 2023-01-31
    相关资源
    最近更新 更多