简体   繁体   English

如何从函数中调用组件

[英]How to call a component from a function

I'm learning React Native and I got in an issue when I used TouchableOpacity . 我正在学习React Native,当我使用TouchableOpacity时,我遇到了一个问题。

I have in App.js component productHandler() method. 我在App.js组件中有productHandler()方法。 I want onPress (when you click on Read More) to call Product component and to display it on the screen, but doesn't work. 我想onPress (当您单击Read More时)调用Product组件并在屏幕上显示它,但不起作用。

When I click on Read More nothing happens. 当我点击Read More时没有任何反应。

class App extends Component {
  productHandler = () => {
    return <Product />;
  };
  render() {
    return (
      <View style={style.header}>
        <View style={style.touchableButtonContainer}>
          <TouchableOpacity
            style={style.touchableButton}
            onPress={this.productHandler}
          >
            <Text style={style.fontText}>Read More</Text>
          </TouchableOpacity>
        </View>
        <Text>This is just a text</Text>
      </View>
    );
  }
}

And this is the Product.js 这就是Product.js

class Product extends Component {
  render() {
    return (
      <View>
        <Text>Product page</Text>
        <Text>Product page</Text>
        <Text>Product page</Text>
        <Text>Product page</Text>
        <Text>Product page</Text>
        <Text>Product page</Text>
      </View>
    );
  }
}

I learn in sandbox so this little code is here . 我在沙盒中学习,所以这个小代码在这里

The issue is that you are not telling react where to render that component. 问题是你没有告诉在哪里渲染该组件。 The better way would be handle state and render based on the condition : 更好的方法是根据条件处理状态和渲染:

class App extends Component {
  state = {
    isActive: false
  };
  productHandler = () => {
    this.setState({ isActive: !this.state.isActive });
  };
  render() {
    return (
      <View style={style.header}>
        <View style={style.touchableButtonContainer}>
          <TouchableOpacity
            style={style.touchableButton}
            onPress={this.productHandler}
          >
            <Text style={style.fontText}>
              {this.state.isActive ? "Hide" : "Read More"}
            </Text>
          </TouchableOpacity>
        </View>
        {this.state.isActive && <Product />}
        {!this.state.isActive && <Text>This is just a text</Text>}
      </View>
    );
  }
}

Here is live demo: https://codesandbox.io/s/react-native-oln9t 这是现场演示: https//codesandbox.io/s/react-native-oln9t

You can't return a component from a click handler like that. 您无法从像这样的单击处理程序返回组件。 Where would you even expect it to display in that case? 在那种情况下,你甚至希望它在哪里展示?

Instead, you need to keep some state, change that state on click, and then conditionally render the Product component based on that state. 相反,您需要保留一些状态,在单击时更改该状态,然后根据该状态有条件地呈现Product组件。

class App extends Component {

  constructor() {
    this.state = { showingProduct: false }
    super()
  }

  productHandler = () => {
    this.setState({ showingProduct: true })
  };

  render() {
    return (
      <View style={style.header}>
        <View style={style.touchableButtonContainer}>
          <TouchableOpacity
            style={style.touchableButton}
            onPress={this.productHandler}
          >
            <Text style={style.fontText}>Read More</Text>
          </TouchableOpacity>

          { this.state.showingProduct && <Product /> }

        </View>
        <Text>This is just a text</Text>
      </View>
    );
  }
}

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

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