在 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,但它不起作用。我知道我必须先做一个函数,但我不知道怎么做。任何帮助将不胜感激。谢谢


米琪卡哇伊
浏览 132回答 2
2回答

慕桂英546537

你试过了吗:&nbsp;&nbsp;&nbsp;<th>&nbsp;${name.arCBValue3&nbsp;?&nbsp;'Yes':'No'}</th>

子衿沉夜

import {&nbsp; style} from "./../../../reports/ARCssPdf";export const arGenerateFileHtml = values => {&nbsp; return `&nbsp; ${style}&nbsp; ${generateNameSectionHtml(values)}`;};const replacer = value => value ? "Yes" : "No"const generateNameSectionHtml = name =>&nbsp; `<html><head></head>&nbsp; &nbsp; <br/><body>&nbsp; <div>&nbsp; &nbsp; &nbsp; <table style="width:450px">&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td> Section IV - AutomobileLiability</td>&nbsp; &nbsp; &nbsp; <th> : </th>&nbsp; &nbsp; &nbsp; <th> ${replacer(name.arCBValue3)}</th> //This is the output&nbsp; &nbsp; &nbsp; </tr>&nbsp; </table></div></body></html>`<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript