首页 技术 正文
技术 2022年11月7日
0 收藏 478 点赞 396 浏览 2366 个字

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

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

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

如何在taro的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 & PageStatePropsinterface 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 & PageStatePropsinterface 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 & PageOwnPropsinterface 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>

 完美解决

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900