如何在taro的map循环中使用if条件渲染
在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条件渲染的更多相关文章
- 教你如何在React及Redux项目中进行服务端渲染
服务端渲染(SSR: Server Side Rendering)在React项目中有着广泛的应用场景 基于React虚拟DOM的特性,在浏览器端和服务端我们可以实现同构(可以使用同一份代码来实现多端 ...
- 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。
原文:如何在WindowsPhone Bing Map控件中显示必应中国中文地图.谷歌中国中文地图. 最近正好有点业余时间,所以在做做各种地图.Bing Map控件本身就能显示必应地图,但是很遗憾微软 ...
- 解决 java循环中使用 Map时 在put值时value值被覆盖的问题
其实很简单,只需要把容器换成list 然后在循环中,每次循环末尾map = new HashMap() 或者直接在循环中一开始就实例化hashmap(Map map = new HashMap();) ...
- Ajax请求php返回json对象数据中包含有数字索引和字符串索引,在for in循环中取出数据的顺序问题
//php中的数组数据格式 Array ( [all] => Array ( [title] => ALL [room_promotion_id] => all ) [best_av ...
- For循环中不可以嵌套RDD操作
今天犯了一个致命理解错误,Spark中的RDD Map操作只是一个计算式的传递,并不是Action,也就是在for循环中不会产生真正的计算. 因此,如果for循环中出现了RDD的Map类似操作,都会引 ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- react map循环数据 死循环
项目条件:react es6 antidesign 已在commonState中获取到list,但是在循环map填充DOM的时候陷入死循环. 原因:因为是子组件 ,在父组件请求数据的时候 有个时差过程 ...
- Day11_55_在Map集合中使用泛型
在Map集合中使用泛型 ``` import java.util.HashMap; import java.util.Iterator; import java.util.Map; import ja ...
- React技巧之中断map循环
正文从这开始~ 总览 在React中,中断map()循环: 在数组上调用slice()方法,来得到数组的一部分. 在部分数组上调用map()方法. 遍历部分数组. export default fun ...
随机推荐
- 关于Unity中从服务器下载资源压缩包AssetBundle的步骤
AssetBundle 1: 在Unity中,能为用户存储资源的一种压缩格式的打包集合,他可以存任意一种Unity引擎可以识别的资源: 模型,音频,纹理图,动画, 开发者自定义的二进制文件; 2: 这 ...
- maven学习日记(三)-------开发环境搭建(springmvc+hibernate4)各种maven错误汇总
1.maven编码 gbk 的不可映射字符 解决这个问题的思路: 在maven的编译插件中声明正确的字符集编码编码——编译使用的字符集编码与代码文件使用的字符集编码一致!! 安装系统之后,一般中文系统 ...
- 第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理
第三百六十四节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的mapping映射管理 1.映射(mapping)介绍 映射:创建索引的时候,可以预先定义字 ...
- 第三百二十四节,web爬虫,scrapy模块介绍与使用
第三百二十四节,web爬虫,scrapy模块介绍与使用 Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了 ...
- zXing使用注意事项-转
zXing使用注意事项 ------------------ zxing和zbar的比较: ZXing项目的示例程序对于摄像头的控制写的非常全面,ZBar的没有ZBar基于C语言编写,解码效率高于 ...
- SPRING---------配置文件的命名空间
两种格式的配置文件: DTD和Schema区别: Schema是对XML文档结构的定义和描述,其主要的作用是用来约束XML文件,并验证XML文件有效性.DTD的作用是定义XML的合法构建模块,它使用一 ...
- application/json 与 application/x-www-form-urlencoded的简单比较
application/x-www-form-urlencoded 提交请求示例 curl -X POST 'http://localhost:8080/formPost' -d 'id=1& ...
- 动态添加的html元素绑定事件的方法
避免先写了DOM操作,但是元素是动态加载的,所以点击不生效,比较好的方法有两个: 1.动态添加的时候加行内事件,比如onclick="funcName()" 在js中写好方法名对应 ...
- vue的iview列表table render函数设置DOM属性值的方法
{ title: '负责人社保照片', key: 'leaderIdNumber', render: (h, params) => { return h('img',{domProps:{ sr ...
- ubuntu 的chmod 和 chown
1.chown改文件或目录的所有者和群组权限 格式 chown [OPTION]... [OWNER][:[GROUP]] FILE... 参数: -R 递归操作当前目录下的所有目录和文件: -h 更 ...