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

函数式组件传参 {参数} 写法报undefind #3675

Closed
h-ecstasy opened this issue Jul 4, 2019 · 5 comments
Closed

函数式组件传参 {参数} 写法报undefind #3675

h-ecstasy opened this issue Jul 4, 2019 · 5 comments
Assignees

Comments

@h-ecstasy
Copy link

问题描述
函数式组件传参时,使用 props 传参,用 props.title 方式可以获取参数 title 的值,但是用 {title} 方法传参时 会报 title is not defind 错误
复现步骤
[复现问题的步骤]
一下是一个简易例子
正常组件

import Taro from '@tarojs/taro';
import { View, Button } from '@tarojs/components';
import PropTypes from 'prop-types';

const PageModal = (props) => {
  console.log(props)
  return (
    <View className='page-modal'>
      <View>{props.title}</View>
      <View>
        {props.children}
      </View>
      <View>
        <Button>{props.cancelTxt}</Button>
        <Button>{props.confirmTxt}</Button>
      </View>
    </View>
  )
}

export default PageModal

报错组件

import Taro from '@tarojs/taro';
import { View, Button } from '@tarojs/components';
import PropTypes from 'prop-types';
import './index.scss';

const PageModal = ({ title, children, cancelTxt, confirmTxt }) => {
  return (
    <View className='page-modal'>
      <View>{title}</View>
      <View>
        {children}
      </View>
      <View>
        <Button>{cancelTxt}</Button>
        <Button>{confirmTxt}</Button>
      </View>
    </View>
  )
}

PageModal.propTypes = {
  title: PropTypes.string,
  cancelTxt: PropTypes.string,
  confirmTxt: PropTypes.string,
}

PageModal.defaultProps = {
  title: '自定义',
  cancelTxt: '取消',
  confirmTxt: '确认'
}

export default PageModal

组件使用

<PageModal
     title='自定义'
     cancelTxt='取消'
     confirmTxt='确认'
>
      <View>弹窗内容</View>
</PageModal>

期望行为
希望可以实现 ({ title, children, cancelTxt, confirmTxt }) 这样的传参方式

报错信息
image
image

系统信息

  • 操作系统: [Windows 10]
  • Taro 版本 [v.1.3.0]
  • Node.js 版本 [v10.9.0]
  • 报错平台 [weapp]
@taro-bot
Copy link

taro-bot bot commented Jul 4, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@yuche
Copy link
Contributor

yuche commented Jul 4, 2019

PageModal.defaultProps = {
  title: '自定义',
  cancelTxt: '取消',
  confirmTxt: '确认'
}

这个写法在函数式组件 1.3.0 还不支持,得升级到 1.3.4

@h-ecstasy
Copy link
Author

imagewo'g
我用 taro update project 更新到1.3.5 还是报上面的错误

@yuche
Copy link
Contributor

yuche commented Jul 4, 2019

cli 更新了吗

@Chen-jj Chen-jj self-assigned this Jul 4, 2019
@h-ecstasy
Copy link
Author

@yuche 非常感谢 cli 更新到1.3.5 以后就没有报错了

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