无法在本机中呈现onPress TouchableOpacity视图

时间:2018-01-10 18:26:57

标签: javascript reactjs react-native

嘿伙计们,请你帮我解决这个问题。 我想在按下TouchableOpacity时渲染视图,但它不起作用。可以请任何建议最好的解决方案。

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>

        );
    }
}

3 个答案:

答案 0 :(得分:1)

目前,您的组件在呈现视图时可以依赖于它们。它可以通过以下方式简单地解决:

  1. 将状态添加到组件,例如:state = { renderView : false }

  2. 添加条件渲染,降低你的状态:

    render(){         回来(

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

    {this.state.renderView&amp;&amp; this._renderMyView()}                     

            );
        }
    
  3. _renderMyView()返回您的观看次数,因此只需为您的观点添加返回陈述。

  4. 按下触发时更改状态: <TouchableOpacity onPress={() => this.setState({renderView: true})} ... >

答案 1 :(得分:0)

你应该像这样返回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>

    );
}

}

答案 2 :(得分:0)

React的工作方式基于状态更改,因此您始终基于当前状态(或道具)进行渲染。 如果需要根据某些更改进行渲染,则应首先更改状态,然后有条件地渲染到新状态:

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

export default class App extends Component {

    constructor(props){
        super(props);

        this.state = {
            renderView: false
        }

        //You had a slighty mistake here, you have to assign bind's result
        this._renderMyView = this._renderMyView.bind(this);
        this._onPress= this._onPress.bind(this);
    }

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

    _onPress() {
       //React to the event and change the state
       this.setState({renderView: true});
   }

    _renderMyView () {
        //Render based on the current state
        if(this.state.renderView) {
            return (
              <View>
                  <Text>I am here to returns camera</Text>
                  <Text>More stuff here...</Text>
              </View>
            );
        }

        return null;
    }
}

为了澄清,每次调用this.setState时,都会执行组件的循环寿命,有关更多信息,请参阅the oficial docs,更新部分。也就是说:

  1. componentWillReceiveProps()
  2. shouldComponentUpdate()
  3. componentWillUpdate()
  4. render()
  5. componentDidUpdate()
  6. 作为附加注释(为了上面的示例,它不是必需的),this.setState异步执行,这意味着如果您需要立即执行某些操作状态被修改之后,您必须将回调作为第二个参数传递:

    this.setState({someProp: 1}, () => {
        console.log('someProp: ' + this.state.someProp); //someProp: 1
    });
    

    这样做会产生意外行为:

    this.setState({someProp: 1});
    //You cannot ensure that the state has changed at this point, so the following line could be the expected or the previous value
    console.log('someProp: ' + this.state.someProp); //someProp: ???