【发布时间】:2019-09-21 14:01:24
【问题描述】:
我有 2 个父组件(LoginScreen)和一个子组件(SignupSection)。子组件有 onClick 按钮。当用户单击按钮时,我需要从父组件触发功能,这对我不起作用。我有以下代码
我想从子组件调用父函数
父母
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} onClick={()=>this.props.childcall()}>Create Account</Text>
<Text style={styles.text}>Forgot Password?</Text>
</View>
);
}
}
【问题讨论】:
-
@errorau 我想要相反的方式从 React Native 中的子组件调用父函数
标签: react-native react-redux jsx