React native 如何通过属性将函数传递给子组件

React native 如何通过属性将函数传递给子组件,react-native,React Native,我想从父组件中获取子组件中TextInput的值 上面有一个例子。但我使用的是模板,TextInput是以不同的方式创建的(至少对我来说,我是react native的新手),如下所示。 我试过这样做,但我无法启动onChangeValue方法 -----Child-------- import { TextInput, View } from 'react-native'; import styled from 'styled-components'; const CustomInpu

我想从父组件中获取子组件中TextInput的值

上面有一个例子。但我使用的是模板,TextInput是以不同的方式创建的(至少对我来说,我是react native的新手),如下所示。 我试过这样做,但我无法启动onChangeValue方法

-----Child--------

import { TextInput, View } from 'react-native';

import styled from 'styled-components';

const CustomInput = styled(TextInput).attrs(({onChangeValue, placeholder, type, theme }) => ({
  placeholderTextColor: theme.colors.transparentGrayx,
  selectionColor: theme.colors.defaultWhite,
  underlineColorAndroid: 'transparent',
  secureTextEntry: type === 'password',
  autoCapitalize: 'none',
  textContentType: type,
  autoCorrect: false,
  placeholder,
  onChangeValue: onChangeValue
}))`
  width: 90%;
  height: 100%;
  font-family: CircularStd-Book;
  color: ${({ theme }) => theme.colors.defaultWhite};
`;



type InputProps = {
  placeholder: string,
  iconName: string,
  type: string,
  onChangeValue : function,
};

const Input = ({ value, placeholder, iconName, type, onChangeValue }: InputProps): Object => (
  <ContentContainer
    color={appStyles.colors.transparentGray}
  >
    <InputWrapper>
      <InputIcon
        iconName={iconName}
      />
      <CustomInput
        placeholder={placeholder}
        type={type}
        value = {value}
        onChangeValue = {onChangeValue}
      />
    </InputWrapper>
  </ContentContainer>
);

export default Input;


-----Child--------

---Parent--------------
.
.

  constructor(props) {
    super(props);
    this.onChangeValue = this.onChangeValue.bind(this);
    this.state = {
      value: ''
    };
  }
.
.

  onChangeValue = e => {
    console.log("dfdf")
  }
.
.
  renderInput = (
    placeholder: string,
    iconName: string,
    type: string,
    style: Object,
    onChangeValue : function
  ): Object => (
    <Input
      placeholder={placeholder}
      iconName={iconName}
      style={style}
      type={type}
      onChangeValue={onChangeValue}
    />
  );

.
.
return (
      <Container>
        <Animated.View
          style={emailAnimationStyle}
        >
          {this.renderInput(
            'E-mail',
            'email-outline',
            'emailAddress',
            emailAnimationStyle,
            this.onChangeValue()
          )}
        </Animated.View>

      </Container>
    );
---Parent--------------
----子对象--------
从“react native”导入{TextInput,View};
从“样式化组件”导入样式化;
const CustomInput=styled(TextInput).attrs({onChangeValue,占位符,类型,主题})=>({
占位符文本颜色:theme.colors.transparentGrayx,
selectionColor:theme.colors.Default白色,
underlineColorAndroid:'透明',
secureTextEntry:type==='password',
自动资本化:“无”,
textContentType:type,
自动更正:错误,
占位符,
onChangeValue:onChangeValue
}))`
宽度:90%;
身高:100%;
字体系列:传阅本;
颜色:${({theme})=>theme.colors.defaultWhite};
`;
类型InputProps={
占位符:字符串,
我的名字:字符串,
类型:字符串,
onChangeValue:函数,
};
常量输入=({value,placeholder,iconName,type,onChangeValue}:InputProps):对象=>(
);
导出默认输入;
-----孩子--------
---母公司--------------
.
.
建造师(道具){
超级(道具);
this.onChangeValue=this.onChangeValue.bind(this);
此.state={
值:“”
};
}
.
.
onChangeValue=e=>{
console.log(“dfdf”)
}
.
.
renderInput=(
占位符:字符串,
我的名字:字符串,
类型:字符串,
样式:对象,
onChangeValue:函数
):Object=>(
);
.
.
返回(
{this.renderInput(
“电子邮件”,
“电子邮件大纲”,
“电子邮件地址”,
电子邮件动画风格,
this.onChangeValue()
)}
);
---母公司--------------
有人能帮我吗。

有一个示例代码。 组成部分:

const UserInput = ({onChangeText}) => (

  <View>
      <TextInput onChangeText={text => onChangeText(text)}></TextInput>
  </View>
)
constuserinput=({onChangeText})=>(
onChangeText(文本)}>
)
下面是组件的用法:

  constructor(props){
    super(props)
    this.onChangeText = this.onChangeText.bind(this)

  }

  onChangeText(text){
    console.log(text)
  }
  render() {
    return (
      <UserInput onChangeText={this.onChangeText}>

      </UserInput>
    )
  }
} 
构造函数(道具){
超级(道具)
this.onChangeText=this.onChangeText.bind(this)
}
onChangeText(文本){
console.log(文本)
}
render(){
返回(
)
}
} 
有一个示例代码。 组成部分:

const UserInput = ({onChangeText}) => (

  <View>
      <TextInput onChangeText={text => onChangeText(text)}></TextInput>
  </View>
)
constuserinput=({onChangeText})=>(
onChangeText(文本)}>
)
下面是组件的用法:

  constructor(props){
    super(props)
    this.onChangeText = this.onChangeText.bind(this)

  }

  onChangeText(text){
    console.log(text)
  }
  render() {
    return (
      <UserInput onChangeText={this.onChangeText}>

      </UserInput>
    )
  }
} 
构造函数(道具){
超级(道具)
this.onChangeText=this.onChangeText.bind(this)
}
onChangeText(文本){
console.log(文本)
}
render(){
返回(
)
}
}