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

taro 中如何初始化props #71

Closed
xuxiaoxiao312 opened this issue Jun 11, 2018 · 2 comments
Closed

taro 中如何初始化props #71

xuxiaoxiao312 opened this issue Jun 11, 2018 · 2 comments

Comments

@xuxiaoxiao312
Copy link

getDefaultProps 并不生效,是否不支持设置props的初始值以及类型?

@yuche
Copy link
Contributor

yuche commented Jun 11, 2018

两种方法:

class Greeting extends Taro.Component {
  static defaultProps = {
    name: 'Stranger'
  };
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

// 或者这样
Greeting.defaultProps = {
  name: 'Stranger'
};

@xgqfrms
Copy link

xgqfrms commented Mar 27, 2020

defaultProps

  1. static
class Greeting extends Taro.Component {
  constructor (props) {
    super(props)
    this.state = {
      name: ``,
    }
  }
  static defaultProps = {
    name: 'Stranger'
  };
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}
  1. Props
class Greeting extends Taro.Component {
  constructor (props) {
    super(props)
    this.state = {
      name: ``,
    }
  }
  render() {
    return (
      <h1>Hello, {this.props.name}</h1>
    );
  }
}

Greeting.defaultProps = {
  name: 'Stranger'
};

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