首页 > 解决方案 > 在 React Native-JavaScript 中将布尔值转换为字符串

问题描述

所以,我在做一个 React Native 项目,我有一个这样的复选框:

      import ARCheckBox from '@react-native-community/checkbox';
      .............
      <View style={styles.row}>
      <Text style={styles.label}>Insured Interest</Text>
         <Text>Section IV - AutomobileLiability</Text>
         <ARCheckBox
          value={this.state.arCBValue3}
          onValueChange={value =>
            this.setState({
              arCBValue3: value,
            })
          }
        />
      </View>

这是我的 .js 输出:

import { style } from "./../../../reports/ARCssPdf";

export const arGenerateFileHtml = values => {
  return`
  ${style}
  ${generateNameSectionHtml(values)}`;
};

const generateNameSectionHtml = name =>
`<html>
<head>
</head>
    <br/>
<body>
  <div>
      <table style="width:450px">
      <tr>
      <td> Section IV - AutomobileLiability</td>
      <th> : </th>
      <th> ${name.arCBValue3}</th> //This is the output
      </tr>
  </table>
</div>
</body>
</html>`

但我得到的结果是这样的:

Section IV - AutomobileLiability : true/false

虽然我想要的结果是这样的:

Section IV - AutomobileLiability : Yes/No

我如何得到这个结果?我尝试使用replaceFunction 将 true/false 替换为 Yes/No,但它不起作用。我知道我必须先创建一个函数,但我不知道如何。任何帮助将不胜感激。谢谢

标签: javascriptreact-nativereact-native-android

解决方案


你试过了吗:

   <th> ${name.arCBValue3 ? 'Yes':'No'}</th>

推荐阅读