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传过来的函数, 子组件能调用, 但父组件不执行(父组件中子组件外层加了判断) #4554

Closed
pollux2015 opened this issue Sep 30, 2019 · 5 comments
Assignees

Comments

@pollux2015
Copy link

pollux2015 commented Sep 30, 2019

问题描述
子组件调用父组件props传过来的函数, 子组件能调用, 但父组件不执行
在父组件中, 子组件外面加了一层判断
如果不加一层判断则可以执行

复现步骤

父组件

{/**** 这种方式可以触发: Qbutton的 onclick事件 ***/}
        <View className='page-create-footer'>
          <Qbutton onClick={this.addNewQuestion.bind(this)} ghost>
            添加下一题
          </Qbutton>
        </View>

        {/**** 外面加了一个判断 ***/}
        {/**** 这种方式无法触发: Qbutton的 onclick事件 ***/}
        {action && (
          <View className='page-create-footer'>
            <Qbutton onClick={this.addNewQuestion.bind(this)} ghost>
              添加下一题
            </Qbutton>
          </View>
        )}

##子组件

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

import Iconfont from '../iconfont/iconfont'
import './q-button.scss'

class Qbutton extends Component {
  static externalClasses = ['my-class']

  static defaultProps = {
    size: 'default',
    color: 'primary',
    icon: null,
    ghost: false,
    long: false,
    noRadius: false,
    onClick() {}
  }

  render() {
    const { size, color, icon, ghost, long, noRadius } = this.props
    const classNames = [
      'q-button',
      'my-class',
      `size-${size}`,
      `color-${color}`,
      !noRadius ? 'radius' : null,
      long ? 'long' : null,
      ghost ? 'ghost' : null
    ]

    const classStr = classNames.filter(name => name).join(' ')
    return (
      // 调用父组件onclick
      <View className={classStr} onClick={this.props.onClick.bind(this)}>
        {icon && <Iconfont my-class='q-button-icon' name={icon} />}
        {this.props.children}
      </View>
    )
  }
}

export default Qbutton

期望行为
在外面加了一层判断后可以执行父组件方法

报错信息

系统信息
Taro CLI 1.3.18 environment info:
System:
OS: macOS 10.15
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 10.13.0 - /usr/local/bin/node
Yarn: 1.7.0 - /usr/local/bin/yarn
npm: 6.11.3 - /usr/local/bin/npm
npmGlobalPackages:
typescript: 3.5.2

补充信息

@taro-bot
Copy link

taro-bot bot commented Sep 30, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@pollux2015
Copy link
Author

我自己已经解决, 这个应该算个BUG

@connect(
  ({ paperCreate }) => ({
    paperCreate
  })
)

....

**这个地方的action props的paperCreate这个对象中没有这个属性导致的 **
const { base, questions, action } = this.props.paperCreate

....

{action && (
          <View className='page-create-footer'>
            <Qbutton onClick={this.onSubmitPaper.bind(this)} ghost>
              {paperId ? '更新试卷' : '提交试卷'}
            </Qbutton>
            <Qbutton onClick={this.addNewQuestion.bind(this)} ghost>
              添加下一题
            </Qbutton>
          </View>
        )}

@pollux2015
Copy link
Author

都没有action 还给我显示出来了 ???

@taro-bot
Copy link

taro-bot bot commented Oct 8, 2019

CC @yuche

@Chen-jj
Copy link
Contributor

Chen-jj commented Jul 2, 2020

Taro3 应该没有这个问题了。

@Chen-jj Chen-jj closed this as completed Jul 2, 2020
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

4 participants