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

state中含有对象时,setState的问题,求教 #1492

Closed
agulado opened this issue Dec 13, 2018 · 4 comments
Closed

state中含有对象时,setState的问题,求教 #1492

agulado opened this issue Dec 13, 2018 · 4 comments
Assignees

Comments

@agulado
Copy link

agulado commented Dec 13, 2018

问题:当page的state含有对象(代码中的obje),且赋值给子组件的prop时(代码中的CompNoConnect),setState()后,page和子组件中的obje相关属性值都没有改变,state.info则改变了,但是通过console得知state确实改了(和 #1162 有点类似) 。
如page中没有子组件CompNoConnect,或是没有传递props,是正常的。
求解。。。
taro 1.2.0-beta.12
https://github.com/agulado/tarojsReport/tree/stateObject/myapp/src

/* pages/index/index */
import Taro, { Component } from '@tarojs/taro'
import { View, Button } from '@tarojs/components'
import CompNoConnect from '../../component/comp-no-connect'

const INITIAL_STATE = {
  info: 'old info',
  obje: {
    title: 'old title',
    content: 'old content'
  }
}

type State = Readonly<typeof INITIAL_STATE>

export default class Index extends Component {

  readonly state: State = INITIAL_STATE

  onButtonClick() {
    this.setState(() => ({
      info: 'new info',
      obje: {
        title: 'new title',
        content: 'new content'
      }
    }), () => {
      console.warn(this.state)
    })
  }

  render() {
    return (
      <View>
        <Button onClick={this.onButtonClick.bind(this)} >update state</Button>
        <View>this.state.title={this.state.info}</View>
        <View>this.state.obje.title={this.state.obje.title}</View>
        <View>this.state.obje.content={this.state.obje.content}</View>
        --分割线(以下为不含connect的组件)--
        <CompNoConnect
          prop_info={this.state.info}
          prop_title={this.state.obje.title}
          prop_content={this.state.obje.content}
        />
      </View>
    )
  }
}
/* component/comp-no-connect */
import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

interface Props {
    prop_info: string,
    prop_title: string,
    prop_content: string
}

export default class CompNoConnect extends Component {

    static defaultProps = {
        prop_info: 'default info',
        prop_title: 'default title',
        prop_content: 'default content'
    }

    constructor(readonly props: Props) {
        super(props)
    }

    render() {
        return (
            <View>
                <View>this.props.info= {this.props.prop_info}</View>
                <View>this.props.title= {this.props.prop_title}</View>
                <View>this.props.content= {this.props.prop_content}</View>
            </View>
        )
    }
}
@taro-bot
Copy link

taro-bot bot commented Dec 13, 2018

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

@taro-bot
Copy link

taro-bot bot commented Dec 13, 2018

CC @Chen-jj

@Chen-jj
Copy link
Contributor

Chen-jj commented Dec 13, 2018

已修复:36b87b6

@Chen-jj Chen-jj closed this as completed Dec 13, 2018
@Chen-jj
Copy link
Contributor

Chen-jj commented Dec 13, 2018

有点问题,还是先开着吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants