简体   繁体   English

如何在React-native中的Render类中调用函数

[英]How to Call a Function inside a Render Class in React-native

I want to call a function inside an imported render class. 我想在导入的渲染类中调用一个函数。 I tried the following but no success. 我尝试了以下方法,但没有成功。

class1.js class1.js

import class2 from "./class2";
export default class1 MainCategoriesScreen extends React.PureComponent  {
    renderItem({ item }) {
       return <Class2 product={item}/>
    }

    changeCategoryId(id){
        console.log(id);
    }

    render() {
     return (
      <FlatList
      data={this.state.products}
      renderItem={this.renderItem}
      ...

} }

and for class2 而对于class2

  render() {
   return (
      <Card style={{flex:1}}>
          <CardItem cardBody button
            onPress={this.changeCategoryId(product.id)}>
            ...
          </CardItem>
            ...
}
export default withNavigation(class2 );

Also I tried these: 我也尝试了这些:

this.changeCategoryId(product.id)
this.changeCategoryId(product.id)
this.changeCategoryId.bind(product.id)
this.props.changeCategoryId(product.id)

You can pass the changeCategoryId method from class 1 to class 2 as a prop, and then call it like this.props.changeCategoryId() : 您可以将changeCategoryId方法传递给class 2作为道具,然后像this.props.changeCategoryId()那样调用它:

// class 1
constructor(props) {
    super(props);

    ...

    this.renderItem = this.renderItem.bind(this);
    this.changeCategoryId = this.changeCategoryId.bind(this);
}

renderItem({ item }) {
   return <Class2 product={item}
                  changeCategoryId={this.changeCategoryId}/>
}

// class 2
  render() {
    return (
      <Card style={{flex:1}}>
        <CardItem cardBody button
          onPress={this.props.changeCategoryId(product.id)}>
          ...
        </CardItem>
      ...

Note that you need to bind both changeCategoryId and renderItem in class 1. 请注意,您需要在类1中同时绑定changeCategoryIdrenderItem

I recently had the same issue, just do this: 我最近遇到了同样的问题,只需执行以下操作:

export default class1 MainCategoriesScreen extends React.PureComponent  {
  renderItem = ({ item }) => {
    return <Class2 product={item} onPress={this.myfunction} />
  }

  myfunction = (id) => {
      console.log(id);
  }

  render() {
    return (
     <FlatList
       data={this.state.products}
       renderItem={this.renderItem}
       ...

and

render() {
  return (
    <Card style={{flex:1}}>
      <CardItem cardBody button
        onPress={() => this.props.onPress(product.id)}>
        ...
      </CardItem>
        ...

You can also try this: 您也可以尝试以下操作:

renderItem = ({ item }) => {
   return <Class2 product={item} onPress={id => this.myfunction(id)} />
}

myfunction(id) {
  console.log(id);
}
renderItem({ item }) {
  return <Class2 product={item}/>
}

You appear to be missing passing the prop into Class2 , which will handle the changeCategoryId. 您似乎缺少将prop传递到Class2的能力 ,它将处理changeCategoryId。

renderItem({ item }) {
  return <Class2 changeCategoryId={this.changeCategoryId} product={item}/>
}

This means, Class2 will now have access to a prop, called changeCategoryId which will be Class1's changeCategoryId function. 这意味着,Class2现在可以访问一个名为changeCategoryId的道具,它将成为Class1的changeCategoryId函数。

Then in the render function within your Class2, you can do: 然后,在Class2中的render函数中,您可以执行以下操作:

<CardItem cardBody button
  onPress={() => this.props.changeCategoryId(product.id)}>
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM