【问题标题】:Error _this.props.childcall is not a function. (In '_this2.props.childcall()' is undefined)错误 _this.props.childcall 不是函数。 (在 '_this2.props.childcall()' 中未定义)
【发布时间】:2019-09-22 05:33:36
【问题描述】:

我有 2 个父组件(LoginScreen)和一个子组件(SignupSection)。子组件有 onClick 按钮。当用户单击按钮时,我需要从父组件触发 childcall() 函数,这对我不起作用。我收到此错误

_this.props.childcall is not a function.
(In '_this2.props.childcall()' is undefined)

我有以下代码:

父母

import React, {Component} from 'react';
import Wallpaper from './Wallpaper';
import SignupSection from './SignupSection';

export default class LoginScreen extends Component {
   constructor(props) {
     super(props)
   }

   childcall()
   {
     alert("hello , this call from child component");
   }

   render() {
     return (
      <Wallpaper>
      <SignupSection 
        childcall ={this.childcall.bind(this)} 
      />
      </Wallpaper>
      );
    }
   }

孩子

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {StyleSheet, View, Text} from 'react-native';


export default class SignupSection extends Component {
    constructor(props) {
       super(props)
    }
    componentWillMount()
    {

    }

    render() {
       return (
          <View style={styles.container}>
            <Text style={styles.text}  onPress={()=>this.props.childcall()}>Create Account</Text>
           <Text style={styles.text}>Forgot Password?</Text>
          </View>
       );
    }
 }

【问题讨论】:

标签: reactjs react-native react-redux jsx


【解决方案1】:

它不起作用,因为您正在渲染函数而不是将其作为道具传递。

你正在通过 children 道具

<SignupSection>
  childcall={this.childcall} 
</SignupSection>

改成

<SignupSection childcall={this.childcall} />

【讨论】:

  • @VincentD'amour 传到哪里去了?
  • @JuniusL。 &lt;SignupSection childcall ={this.childcall.bind(this)} /&gt;我可能遗漏了一些东西,但这是问题中的代码
猜你喜欢
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2020-06-01
  • 1970-01-01
  • 2020-11-07
  • 2015-02-24
  • 1970-01-01
相关资源
最近更新 更多