Javascript 如何制作;“组件的条件”;以自然反应

Javascript 如何制作;“组件的条件”;以自然反应,javascript,android,react-native,conditional-statements,Javascript,Android,React Native,Conditional Statements,我想设置一个条件,如果this.props==“0”,组件将显示在屏幕上,否则,组件将隐藏。怎么做 我试过使用react National condition,但不起作用 这是我的代码: {data.is_approved === '0' ? ( <TouchableOpacity onPress = {() => this.deleteTrancation(data.id, data.sales_id)} style={[styles.icons, common

我想设置一个条件,如果this.props==“0”,组件将显示在屏幕上,否则,组件将隐藏。怎么做

我试过使用react National condition,但不起作用

这是我的代码:

{data.is_approved === '0' ? (
  <TouchableOpacity
    onPress = {() => this.deleteTrancation(data.id, data.sales_id)}
    style={[styles.icons, common.backgroundWarn]}
  >
    <Icon name="clear" size={18} color={color.colorOff} />
  </TouchableOpacity>
) : (
  <Text style={[common.textValid]}></Text>
)}
{data.u是否已批准=='0'(
this.deleteTransation(data.id,data.sales_id)}
style={[style.icons,common.backgroundarn]}
>
) : (
)}
我的完整代码:


我希望当字符串“0”时,组件将显示,否则,组件将隐藏,看它是否工作。您可能错过了整数类型 字符串类型

{data.is_approved === 0 ? (
                  <TouchableOpacity
                      onPress = {() => this.deleteTrancation(data.id, data.sales_id)}
                      style={[styles.icons, common.backgroundWarn]}>
                    <Icon name="clear" size={18} color={color.colorOff} />
                  </TouchableOpacity>
                  ) : (
                    <Text style={[common.textValid]}></Text>
                  )}
{data.u是否已批准===0(
this.deleteTransation(data.id,data.sales_id)}
style={[style.icons,common.backgroundarn]}>
) : (
)}

它对任何条件0的作用都是字符串或数字

{
data.is_approved==“0”
?
:

}
我已经为您创建了一个工作示例的codesandbox示例

第一组分


您应该将一些标志(如ShowSecondComponent)传递给第一个组件中的第二个组件

在您的第一个组件代码中


更改第一个组件的renderSecond状态{renderSecond&&}

什么是
this.props.data
?对象?是的,先生,this.props.data是对象那么您的条件是正确的。这里有什么问题?当data.is\u approved为字符串“0”时,将显示touchableOpacity。当data.is\u approved为字符串“1”时,TouchableOpacity将从屏幕上隐藏,那么
呢?当您希望此项可见时?对于条件,使用APIUse类型转换中的字符串类型。Integer.parseInt(字符串)但目前,data.is_approved有未定义的数据类型我的代码错了吗?首先你有console.log(data.is_approved)返回未定义当你从道具接收到数据时,它没有接收请检入道具数据接收或不抱歉,我可以在哪里检查道具数据?
import React, { Component, Fragment } from "react";
import SecondComp from "./SecondComp";

class FirstComp extends Component {
  state = {
    renderSecond: true
  };

  render() {
    const { renderSecond } = this.state;
    return (
      <Fragment>
        Change renderSecond state of FirstComp {renderSecond && <SecondComp />}
      </Fragment>
    );
  }
}

export default FirstComp;
import React, { Component } from "react";

class SecondComp extends Component {
  render() {
    return <div>Rendering Second Component</div>;
  }
}

export default SecondComp;
 state = {
        renderSecond: true
      };
You can change the renderSecond value to false and second component will not render.
Please check the link for working sample and you can even pass this value as prop from the parent component