xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

taro defaultProps

https://nervjs.github.io/taro/docs/best-practice.html#给组件设置-defaultprops


import Taro, { Component, Config } from '@tarojs/taro'
import {
  View,
  Text,
  Image,
  Icon,
  Button,
  Swiper,
  SwiperItem,
} from '@tarojs/components'

import './index.scss'


const log = console.log;

export default class EventPopper extends Component {
  // config = {
  //   navigationBarTitleText: ''
  // }
  // constructor (props) {
  //   super(props)
  //   this.state = {
  //     name: null,
  //     // name: '',
  //     // name: 'Hello world!',
  //   }
  // }
  constructor () {
    super(...arguments)
    this.state = {
      // name: null,
      // name: '',
      name: 'Hello world!',
    }
    this.preventPop = this.preventPop.bind(this);
  }
  static defaultProps = {
    title: 'default prop title 1',
  }

  // 你可以通过 bind 传入多个参数
  preventPop (name, test, e) {
    //事件对象 e 要放在最后
    e.stopPropagation();
    log(`name =
`, name)
    log(`test =
`, test)
    log(`e =
`, e)
  }

  render () {
    return(
      <Button onClick={(e) => this.preventPop(this.state.name, 'test name', e)}>
        事件 button {this.props.title}
      </Button>
    )
    // return(
    //   <Button onClick={this.preventPop.bind(this, this.state.name, 'test name')}>
    //     事件 button {this.props.title}
    //   </Button>
    // )
  }
}

EventPopper.defaultProps = {
  title: 'default prop title 2',
};


OK

bug

state !== props

https://github.com/NervJS/taro/issues/71#issuecomment-604846671

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'
};


全局变量

https://nervjs.github.io/taro/docs/best-practice.html#全局变量


原文地址:https://www.cnblogs.com/xgqfrms/p/12597241.html