Ошибка _this.props.childcall не является функцией. (В _this2.props.childcall () 'не определено) - PullRequest
0 голосов
/ 03 мая 2019

У меня есть 2 компонента родительский (LoginScreen) и дочерний (RegistrationSection).Дочерний компонент имеет кнопку onClick.Когда пользователь нажимает кнопку, мне нужно запустить функцию childcall () из родительского компонента, и это не работает для меня.Я получаю эту ошибку

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

enter image description here

У меня есть следующий код:

Parent

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

1 Ответ

0 голосов
/ 03 мая 2019

Это не работает, потому что вы рендерируете функцию вместо того, чтобы передавать ее как реквизит.

Вы передаете внутри дочернюю опору

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

измените ее на

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