【发布时间】: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>
);
}
}
【问题讨论】:
-
父子组件是否必须在同一个 .js 文件中 @JuniusL。它对我不起作用我收到上面的错误
-
你能把你的项目移到小吃店吗,并分享一个链接。
-
@JuniusL 这里是链接snack.expo.io/@amerbearat/borroup
-
什么都不显示
标签: reactjs react-native react-redux jsx