taro 如何使用dom_如何在taro的map循环中使用if条件渲染

news/2024/5/20 5:34:42

在taro的jsx中,鉴于编译的机制,官方明确的表示了不能在map循环中使用if循环,

但是呢,官方也给出了解决办法,那就是提取变量或者是用三目运算嵌套的方法:

链接奉上:https://github.com/NervJS/taro/blob/master/packages/eslint-plugin-taro/docs/if-statement-in-map-loop.md

但是我再想,如果我有多个条件去判断呢,难道我只能去进行三目套三目吗?

如下(使用的简单的ts):

import Taro, {Component} from '@tarojs/taro'

import {View, Text, Button} from '@tarojs/components'

import connect from "../../containers/counter"

import {ComponentClass} from "react";

type PageOwnProps = {

}

type PageStateProps = {}

type PageState = {

listArr: string[]

}

type IProps = PageOwnProps & PageStateProps

interface List {

props: IProps,

state: PageState

}

@connect

class List extends Component {

constructor() {

super(...arguments);

this.state = ({

listArr: ["one", "two", "three"]

})

}

public render() {

return (

{

this.state.listArr.map((item, index) => {

return index === 0 ?

index =0 item is {item} :

index === 1 ?

index = 1 item is {item} :

null

})

}

)

}

}

export default List as ComponentClass

确实可以达到效果,但是这样写起来层级嵌套的很深了,很不好看,在咨询了taro作者隔壁老李以后,把循环的内容抽出来做子组件,把index和item,当作参数传递给子组件,在子组件里面使用if即可:

import Taro, {Component} from '@tarojs/taro'

import {View, Text, Button} from '@tarojs/components'

import connect from "../../containers/counter"

import {ComponentClass} from "react";

import ListItem from './listItem'

type PageOwnProps = {

}

type PageStateProps = {}

type PageState = {

listArr: string[]

}

type IProps = PageOwnProps & PageStateProps

interface List {

props: IProps,

state: PageState

}

@connect

class List extends Component {

constructor() {

super(...arguments);

this.state = ({

listArr: ["one", "two", "three"]

})

}

public render() {

return (

{this.state.listArr.map((item, index) => {

return

})}

)

}

}

export default List as ComponentClass

子组件listItem.tsx:

import {ComponentClass} from 'react'

import {Component} from '@tarojs/taro'

import {View} from '@tarojs/components'

type PageStateProps = {

counter: {}

}

type PageDispatchProps = {}

type PageOwnProps = {

propIndex: number,

propItem: any

}

type PageState = {}

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface ListItem {

props: IProps;

state: PageState

}

class ListItem extends Component implements ListItem {

render() {

let resultDom: any = null;

if (this.props.propIndex === 2) {

resultDom =

prop is 2 ,item is {this.props.propItem}

}else{

resultDom =

prop is no 2 ,item is {this.props.propItem}

}

return (

{resultDom}

)

}

}

export default ListItem as ComponentClass

完美解决


http://www.niftyadmin.cn/n/3093661.html

相关文章

Flask+JQuery Validation 插件+Ajax进行表单的验证

在Flask web开发实践中,常常会涉及到表单的提交和验证,flask的插件flask-wtf提供了很多表单验证的方法,但是目前所知这些表单验证都需要提交后再验证,不能做到当用户输入完用户名就能检查用户名格式是否正确 最近学习了点Jquery和…

cell自适应高度的简单写法

2019独角兽企业重金招聘Python工程师标准>>> self.myTableView.estimatedRowHeight 70; self.myTableView.rowHeight UITableViewAutomaticDimension; 转载于:https://my.oschina.net/u/2519763/blog/854030

Windows 10 Mobile 在 WP 阵营获得 4.7% 份额

著名广告服务商AdDuplex今天发布了Windows Phone的市场数据,数据抓取9月21日当天24小时的状况。其中比较值得关注的是,Windows 10 Mobile占有率在Windows手机阵营中相较上个月又有上涨,达到了4.7%,相比上个月涨了1.4%。考虑到该系…

hibernate笔记(二)ORM基础

2019独角兽企业重金招聘Python工程师标准>>> 对象关系映射(ORM):Hibernate ORM,其作用就是去映射对象和关系型数据库的,以达到程序中的业务逻辑和数据访问组件相分离。 Hibernate并不能提高对数据库的查询性能,我个人觉得使用它可…

陈松松:新手学习视频制作先学什么软件比较合适

每个视频,都是你的金牌业务员这是我写的第68篇视频营销原创文章与其搜索十年,不如花一年的时间学习,去赚9年的高薪!最近几天上网不方便,没有及时更新视频营销原创文章和视频,也没有及时通知大家&#xff0c…

请领导批阅文件怎么说_搞笑段子:闺蜜去领导车里拿文件,半个小时后,她扶着腰说…...

1:男孩手卡在了共享单车的链条里,疼得大哭,妈妈心疼得眼泪簌簌往下落。消防员叔叔赶来,拿出钳子开始剪链条,小男孩的哭声没有丝毫减弱:叔叔,别剪,疼!消防员安慰男孩&…

Android使用Fragment,不能得到Fragment内部控件,findViewById()结果是Null--已经解决

程序很easy。好长时间没有搞定。郁闷。。。。。。。。。。。。在论坛咨询,最终找到答案。 描写叙述: 一个Activity:MainActivity。内部是一个Fragment:FragmentA。FragmentA里面有TextView。 问题:不管怎样也得不到Fra…

JavaScript其它特殊对象

2019独角兽企业重金招聘Python工程师标准>>> Navigator: 说明: navigator对象是识别客户端浏览器的标准,每个浏览器中的navigator对象有一套自己的属性 属性名称属性说明appName完整的浏览器名称cookieEnabled表示cookie是否启用javaEnabled表示java是否启用languag…