Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

只能在 render 函数中 定义 JSX #848

Closed
xiao0502 opened this issue Oct 16, 2018 · 3 comments
Closed

只能在 render 函数中 定义 JSX #848

xiao0502 opened this issue Oct 16, 2018 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@xiao0502
Copy link

问题描述
列表渲染时,根据列表数组长度判断是否渲染情感图,运用三元运算符时,只能把代码写在里面,不能单独提取出来用函数执行吗?

[或者可以直接贴源代码,能贴文字就不要截图]

// 细节代码写在表达式里面是可以正常渲染的
render() {
    let len = this.state.addressList.length;
    return (
        <View>
            {
                len ? <View className="address-list">
                    {
                        this.state.addressList.map((item, index) => {
                            return (
                                <View key={index} className="address-item">
                                    <View className="hospital-name">{item.hospitalName}</View>
                                    <View className="hospital-info">
                                        <Text>{item.administrative}</Text>
                                        <Text>{item.inpatient}</Text>
                                        <Text>{item.sickbed}</Text>
                                    </View>
                                    <View className="user-info">
                                        <Text>{item.userName}</Text>
                                        <Text>{item.userMobile}</Text>
                                    </View>
                                </View>
                            )
                        })
                    }
                </View>: <View>123</View>
            }
        </View>
    )
}

renderList = () => {
    return (

    )
}

renderEmpty = () => {
    return <View>123</View>
}

// 细节代码用方法执行就渲染报错了

render() {
    let len = this.state.addressList.length;
    return (
        <View className={
            getIsIphoneX() ? 'page-wrapper page-addressManager IphoneX' : 'page-wrapper page-addressManager'}>
            {
                len ? this.renderList(): this.renderEmpty()
            }
        </View>
    )
}

renderList = () => {
    return (
        <View className="address-list">
            {
                this.state.addressList.map((item, index) => {
                    return (
                        <View key={index} className="address-item">
                            <View className="hospital-name">{item.hospitalName}</View>
                            <View className="hospital-info">
                                <Text>{item.administrative}</Text>
                                <Text>{item.inpatient}</Text>
                                <Text>{item.sickbed}</Text>
                            </View>
                            <View className="user-info">
                                <Text>{item.userName}</Text>
                                <Text>{item.userMobile}</Text>
                            </View>
                        </View>
                    )
                })
            }
        </View>
    )
}

renderEmpty = () => {
    return <View>123</View>
}

期望行为
希望能支持函数渲染具体代码

报错信息

image

@yuche yuche changed the title 条件渲染不能执行函数 只能在 render 函数中 定义 JSX Oct 16, 2018
@yuche yuche added the enhancement New feature or request label Oct 16, 2018
@yuche yuche self-assigned this Oct 16, 2018
@yuche
Copy link
Contributor

yuche commented Oct 16, 2018

有一条 eslint 的规则定义了这个行为:
https://github.com/NervJS/taro/blob/master/packages/eslint-plugin-taro/docs/no-jsx-in-class-method.md

@codingwesley
Copy link

还是很赞了 比写原生小程序的语法好

@yuche
Copy link
Contributor

yuche commented Nov 21, 2018

Duplicate of #764

@yuche yuche marked this as a duplicate of #764 Nov 21, 2018
@yuche yuche closed this as completed Nov 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants