taro icon indicating copy to clipboard operation
taro copied to clipboard

render 函数里新建变量,二次修改为 `undefined` 时,没有正确渲染结果

Open Dithub2016 opened this issue 8 years ago • 7 comments

问题描述 在 更改了 coupons 里面一项的 isActive 后,渲染出来的 currentCouponText 的内容正确被更改, 但把所有项目的 isActive 置为 false 的话,渲染没有被改变 (在 h5 端运行正常,小程序端异常)

复现步骤

render () {
    const {coupons} = this.state;
    const currentCoupon = coupons.find(item => item.isActive);
    console.log(currentCoupon)
    const currentCouponText = currentCoupon ? (
      <View className="store-confirm-coupon___value" onClick={this.onShowCouponsPopUp}>
        <Text>已使用 {currentCoupon.show_title}</Text>
        <Image className="arrow" src=''></Image>
      </View>
    ) : (
      <View className="store-confirm-coupon___value none" onClick={this.onShowCouponsPopUp}>
        <Text>无</Text>
        <Image className="arrow" src=''></Image>
      </View>
    );
   return (
      <View>{currentCouponText}</View>
  );
}

期望行为 bug 修复

报错信息 三次选择的 console.log(currentCoupon) 输出值

index.js:203 {id: 1, show_title: "10 元爆款券", isActive: true}
index.js:203 {id: 2, show_title: "5 元爆款券", isActive: true}
index.js:203 undefined

以下为渲染结果 更改前 image 替换 isActive 的话,是正常的 image 所有 isActive 项置为 fasle 后 image

系统信息

  • 操作系统: win7 64bit
  • Taro 版本 v.0.0.67
  • Node.js 版本 v8.11.2

补充说明 更改的操作并非控制台操作,是代码运行 setState 的结果

Dithub2016 avatar Jul 04 '18 06:07 Dithub2016

还有个问题, 如下代码是无法在 render return 的 JSX 里面得到 someThing 的

render() {
  const {state} = this;
  return <View>{state.someThing}</View>
}

Dithub2016 avatar Jul 04 '18 06:07 Dithub2016

第二个问题应该和 https://github.com/NervJS/taro/issues/202 有关

请问你的 this.state.someThing 是不是没有在类属性或构造器声明,是在后期 setState 上去的?

yuche avatar Jul 04 '18 06:07 yuche

这样子,已经初始化的,拿到的是 undefined

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

export default class AddressWithOrder extends Component {
  constructor(...rest) {
    super(...rest)
    this.state = {
      someThing: '123123',
    }
  }

  render () {
    const {state} = this;
    return (
      <View className='address-order'>
        {state.someThing + ''}
      </View>
    )
  }
}

Dithub2016 avatar Jul 04 '18 07:07 Dithub2016

即使是 props 也是不行的

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

export default class AddressWithOrder extends Component {
  constructor(...rest) {
    super(...rest)
  }

  render () {
    const {props} = this;
    return (
      <View className='address-order'>
        {props.someThing + ''}
      </View>
    )
  }
}
AddressWithOrder.defaultProps = {
  someThing: 123,
}

输出: image

Dithub2016 avatar Jul 04 '18 07:07 Dithub2016

了解了,看来 #202 修复了那个 issue 描述的情况,但你这个情况的确没有处理。

yuche avatar Jul 04 '18 07:07 yuche

方便的话你可以再提一个 state & props 关键词的 issue。 这两个在 taro 里的确是关键词需要特殊处理。

yuche avatar Jul 04 '18 07:07 yuche

#264 state & props 关键字在 render 方法里的异常

第二个问题已新建 issue

Dithub2016 avatar Jul 04 '18 07:07 Dithub2016