javascript - 无法在 React Native 中呈现 onPress TouchableOpacity View

标签 javascript reactjs react-native

我想在按下 TouchableOpacity 时呈现一个 View ,但它不起作用。任何人都可以提出最佳解决方案吗?

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

export default class App extends Component {
   
    constructor(props){
        super(props);
        this._renderMyView.bind(this);
    }

    _renderMyView = () => {
      <View>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
          <Text>I am here to returns camera</Text>
      </View>
    }

    render() {
        return (
       
                <TouchableOpacity  onPress={this._renderMyView} style={{height:50,width:50,backgroundColor:'red'}} >
                    <Text style={{height:50,width:50,backgroundColor:'red'}} >camera</Text>
                </TouchableOpacity>

        );
    }
}

最佳答案

你应该像这样返回 JSX。 也给flex 1查看

import React, { Component } from 'react';


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

export default class App extends Component {

constructor(props){
    super(props);
    this.state = {
      visible: false,
    }
}

_renderMyView = () => {
  return (
    <View style={{height: 50, backgroundColor: '#eee',marginTop: 100}}>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
        <Text>I am here to returns camera</Text>
    </View>
  );

}

render() {
    return (

            <View style={{flex: 1}}>
              <TouchableOpacity  onPress={()=>{this.setState({visible: true})}} style={{marginTop: 50,height:50,width:50,backgroundColor:'red'}} >
                  <Text style={{height:50,width:50,backgroundColor:'red'}} >camera</Text>
              </TouchableOpacity>

              {this.state.visible ? this._renderMyView() : null}
            </View>

    );
}

关于javascript - 无法在 React Native 中呈现 onPress TouchableOpacity View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48193925/

相关文章:

javascript - 如何使用ant build在js文件中附加字符串

reactjs - ComponentDidMount 被调用多次

reactjs - 样式组件不带 Prop

javascript - ReactNative - NativeBase - 列表显示为网格

javascript - 每第二次运行都会抛出 : MongoError: Topology was destroyed

javascript - 在文本后保持相同的高度和宽度 ('' )

javascript - 如何列出 jQuery 绑定(bind)的事件?

reactjs - token 应保存在前端的位置(reactjs)

react-native - 为什么 react-native 边框半径适用于 iOS 而不是 Android Image

javascript - React Native <Modal/> onDismiss 未被调用