在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 (
<View className={'index'}>
{
this.state.listArr.map((item, index) => {
return index === 0 ?
<View>index =0 item is {item}</View> :
index === 1 ?
<View>index = 1 item is {item}</View> :
null
})
}
</View>)
}
} export default List as ComponentClass<PageOwnProps, PageState>

 确实可以达到效果,但是这样写起来层级嵌套的很深了,很不好看,在咨询了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 (
<View className={'index'}>
{this.state.listArr.map((item, index) => {
return <ListItem propIndex={index} propItem={item}>
</ListItem>
})}
</View>)
}
} export default List as ComponentClass<PageOwnProps, PageState>

子组件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 = <View>
prop is 2 ,item is {this.props.propItem}
</View>
}else{
resultDom = <View>
prop is no 2 ,item is {this.props.propItem}
</View>
}
return (
<View>
{resultDom}
</View>
)
}
} export default ListItem as ComponentClass<PageOwnProps, PageState>

 完美解决

如何在taro的map循环中使用if条件渲染的更多相关文章

  1. 教你如何在React及Redux项目中进行服务端渲染

    服务端渲染(SSR: Server Side Rendering)在React项目中有着广泛的应用场景 基于React虚拟DOM的特性,在浏览器端和服务端我们可以实现同构(可以使用同一份代码来实现多端 ...

  2. 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。

    原文:如何在WindowsPhone Bing Map控件中显示必应中国中文地图.谷歌中国中文地图. 最近正好有点业余时间,所以在做做各种地图.Bing Map控件本身就能显示必应地图,但是很遗憾微软 ...

  3. 解决 java循环中使用 Map时 在put值时value值被覆盖的问题

    其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...

  4. Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题

    //php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_av ...

  5. For循环中不可以嵌套RDD操作

    今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...

  6. map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法

    package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...

  7. react map循环数据 死循环

    项目条件:react es6 antidesign 已在commonState中获取到list,但是在循环map填充DOM的时候陷入死循环. 原因:因为是子组件 ,在父组件请求数据的时候 有个时差过程 ...

  8. Day11_55_在Map集合中使用泛型

    在Map集合中使用泛型 ``` import java.util.HashMap; import java.util.Iterator; import java.util.Map; import ja ...

  9. React技巧之中断map循环

    正文从这开始~ 总览 在React中,中断map()循环: 在数组上调用slice()方法,来得到数组的一部分. 在部分数组上调用map()方法. 遍历部分数组. export default fun ...

随机推荐

  1. Java如何处理运行时异常?

    在Java编程中,如何处理运行时异常? 此示例显示如何处理java程序中的运行时异常. package com.yiibai; public class RuntimeExceptions { sta ...

  2. TPshop隐藏index.php

    有些朋友提到关于TPshop 隐藏index.php 一问题, 可以修改 Application\Common\Conf\config.php 文件代码 'common', 'AUTH_CODE' = ...

  3. CURL 常用参数

    在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具. 1.查看响应头信息: -I :显示http response的头信息. [root@l ...

  4. mysql 中 时间和日期函数

    From: http://www.cnblogs.com/redfox241/archive/2009/07/23/1529092.html 一.MySQL 获得当前日期时间 函数 1.1 获得当前日 ...

  5. 内存管理 初始化(六)vmalloc_init 及 ioremap

    是不是我错了,本想这个函数会如网上所说将进行非连续内存管理的初始化,但是对于2.6.34的ARM架构而言,该函数实际完成的业务非常少. 内存管理的初始化读到此处,我感觉原有的认识存在很大缺陷: (1) ...

  6. Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果

    前言 前两次,我们学习了 Android Material Design控件学习(一)--TabLayout的用法 Android Material Design控件学习(二)--Navigation ...

  7. 父组件中vuex方法更新state,子组件不能及时更新并渲染的解决方法

    场景: 我实际用到的是这样的,我父组件引用子组件related,父组件调用获取页面详情的方法,更新了state值related,子组件根据该related来渲染相关新闻内容,但是页面打开的时候总是先加 ...

  8. 代码记录——phase16,block36(修正后)

    加入边缘判定,<2则加2. if (x_upleft<2) x_upleft=x_upleft+2; if (y_upleft<2) y_upleft=y_upleft+2; HRE ...

  9. phpcms v9模板制作教程(转载)

    第一节 1.首先下载phpcms v9的集成安装包并安装,这里就不详细说明了. 2.本地调试建议大家使用APMserver,或者wampserver等,可以到PHPCMS吧官方网站首页链接下载.安装好 ...

  10. 【2018年12月10日】A股最便宜的股票

    新钢股份(SH600782) - 当前便宜指数:196.21 - 滚动扣非市盈率PE:2.86 - 动态市净率PB:0.95 - 动态年化股息收益率:1.78% - 新钢股份(SH600782)的历史 ...