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

组件props更新为null不生效 #1151

Closed
wants to merge 1 commit into from

Conversation

flappyjet
Copy link

//components/chart/index.js
import Taro, { Component } from '@tarojs/taro'
export class Chart extends Component {
  render() {
    const { width, height } = this.props;
    const innerStyle = (width && height)? `background-color: red; width: ${width}px; height: ${height}px;`: null;
    return (<View style={innerStyle}>123</View>)
  }
}
//page/index/index.js
import Taro, { Component } from '@tarojs/taro'
import { Chart } from '../../components/chart/index'

export default class Index extends Component {
  config = {
    navigationBarTitleText: '首页'
  }
  state = {
    width:300, height:200,
  }
  handleChangeNostyle = () =>
  {
    this.setState({width:null,height:null})
  }
  handleChangeFullscreen = () =>
  {
    this.setState({width:375,height:500})
  }

  render () {
    return (
      <View>
        <View style="display:flex;width:100%;">
          <View style="flex:1;text-align:center;border:1px solid #ccc;" onClick={this.handleChangeNostyle}>origin</View>
          <View style="flex:1;text-align:center;border:1px solid #ccc;" onClick={this.handleChangeFullscreen}>100%</View>
        </View>
        <Chart width={this.state.width} height={this.state.height} />
      </View>
    )
  }
}

@Chen-jj
Copy link
Contributor

Chen-jj commented Nov 21, 2018

@flappyjet 谢谢你的 PR,问题的确出在这里。但你的 commit 在条件判断有误和对 defaultProps 有影响,所以我另外改了版哈。

@flappyjet
Copy link
Author

@flappyjet 谢谢你的 PR,问题的确出在这里。但你的 commit 在条件判断有误和对 defaultProps 有影响,所以我另外改了版哈。

@Chen-jj 不好意思,请说明一下我的pr什么情况会出错?你的commit里defaultProps有非null值的话会覆盖props为null的,应该是只有props为undefined时defaultProps生效才对吧?

@Chen-jj
Copy link
Contributor

Chen-jj commented Nov 21, 2018

原因是小程序 data 里项的值不可以是 undefined,小程序文档 setData 部分有说。所以在定义组件 properties 的时候是 { type: null, value: null }(create-component 文件里的 bindProperties 函数),可以看到默认值为 null,undefined 是不会出现的。

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

Successfully merging this pull request may close these issues.

2 participants