【ReactNative】代码学习(一)

本文档详细介绍了在ReactNative中如何自定义组件,并根据传入参数显示不同消息。通过`getMessage`函数动态生成`TouchableHighlight`组件,实现登录与注册按钮切换。在`render`方法中,根据props设置显示内容,如`passwordCheckbox`。学习了如何在jsx中添加注释及使用变量记录jsx标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 代码分析:https://github.com/bartonhammond/snowflake
/**
  1.定制控件参数来区分显示内容
  2.过关开关来调用jsx标签变量
**/


/**
  *  Get the appropriate message for the current action
  *  @param messageType FORGOT_PASSWORD, or LOGIN, or REGISTER
  *  @param actions the action for the message type


    1.通过变量形式记录jsx标签,通过参数处理
    2.变量都使用let
  */
  getMessage (messageType, actions) {
    let alreadyHaveAccount =
      <TouchableHighlight
        onPress={() => {
          actions.loginState()
          Actions.Login()
        }} >
        <Text>{I18n.t('LoginRender.already_have_account')}</Text>
      </TouchableHighlight>


    let register =
      <TouchableHighlight
        onPress={() => {
          actions.registerState()
          Actions.Register()
        }} >
        <Text>{I18n.t('LoginRender.register')}</Text>
      </TouchableHighlight>


    switch (messageType) {
      case LOGIN:
        return alreadyHaveAccount
      case REGISTER:
        return register
    }
  }


  /**
   * ### render
   * Setup some default presentations and render
   * 风格:都没有使用分号结束
   * 风格:变量提前声明
   * 定制:根据传入的参数,来显示不同的jsx标签
   */
  render () {
    var leftMessageType = this.props.leftMessageType
    var rightMessageType = this.props.rightMessageType
    var displayPasswordCheckbox = this.props.displayPasswordCheckbox


    var passwordCheckbox = <Text />
    let leftMessage = this.getMessage(leftMessageType, this.props.actions)
    let rightMessage = this.getMessage(rightMessageType, this.props.actions)


    /**
     * Toggle the display of the Password and PasswordAgain fields
     */
    if (displayPasswordCheckbox) {
      passwordCheckbox =
        <ItemCheckbox
          text={I18n.t('LoginRender.show_password')}
          disabled={this.props.auth.form.isFetching}
          onCheck={() => {
            this.props.actions.onAuthFormFieldChange('showPassword', true)
          }}
          onUncheck={() => {
            this.props.actions.onAuthFormFieldChange('showPassword', false)
          }}
      />
    }


    /**
     * The LoginForm is now defined with the required fields. 
     * 学习:可以在render上方添加注释,因为jsx标签中无法添加注释
     */
    return (
      <View style={styles.inputs}>
        <LoginForm
          formType={formType}
          form={this.props.auth.form}
          value={this.state.value}
          onChange={self.onChange.bind(self)} />
     {passwordCheckbox}
      </View>
    )
  }






  // 调用
  // 通过枚举类型来区分左右显示内容
  <LoginRender
      ...
      displayPasswordCheckbox={false} // 是否显示【记住密码】
      leftMessageType={REGISTER}    // 左边显示【注册】
      rightMessageType={LOGIN}      // 右边显示【登录】
      ...
    />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值