简体   繁体   English

React Native:如何将参数从子组件道具传递给父组件

[英]React Native: How to pass params to parent component from child component prop

I want to create a HOC that have a event trigger when a certain key is pressed.我想创建一个在按下某个键时具有事件触发器的 HOC。 When this key is pressed it should provide an event to the parent component.当按下此键时,它应该向父组件提供一个事件。 In this case, the key is "@".在这种情况下,键是“@”。

Child HOC儿童 HOC

import React, { Component } from 'react';

const withMention = WrappedComponent => {
  return class extends Component {
    state = {
      mentionStart: false,
      textInput: '',
      selection: 0,
    };
    handleOnKeyPress = key => {
      if (key === '@') {
        this.setState({ mentionStart: true });
      }
    };

    render() {
      const { onMentionStart } = this.state;
      return (
        <WrappedComponent
          onChangeText={text => {
            this.setState({ textInput: text });
          }}
          onKeyPress={event => this.handleOnKeyPress(event.nativeEvent.key)}
          onSelectionChange={event =>
            this.setState({ selection: event.nativeEvent.selection })
          }
          onMentionStart={onMentionStart}
          {...this.props}
        />
      );
    }
  };
};

export default withMention;

Parent component父组件

const UserComment = withMention(TextInput);

<UserComment onMentionStart={(event) => console.log(event)}       />

I know the implementation is wrong, because whenever I assign a function to onMentionStart prop of child component in parent, child's function is overridden by parent.我知道实现是错误的,因为每当我将一个函数分配给父级子组件的 onMentionStart 道具时,子级的函数就会被父级覆盖。 In this case, how to create a custom event trigger from child component and pass event into it so that the parent can use it accordingly?在这种情况下,如何从子组件创建自定义事件触发器并将事件传递给它,以便父组件可以相应地使用它?

I actually solved it by removing onMentionStart prop from HOC and passed onMentionStart function from parent to child as a callback, handled it in onKeyPress handler function.我实际上是通过从 HOC 中删除 onMentionStart 道具来解决它的,并将 onMentionStart 函数从父级传递给子级作为回调,在 onKeyPress 处理程序函数中处理它。

import React, { Component } from 'react';

const withMention = WrappedComponent => {
  return class extends Component {
    state = {
      mentionStart: false,
      textInput: '',
      selection: 0,
    };
    handleOnKeyPress = key => {
      if (key === '@') {
        this.setState({ mentionStart: true }, () =>
          this.props.onMentionStart(this.state.mentionStart),
        );
      }
    };

    render() {
      return (
        <WrappedComponent
          onChangeText={text => {
            this.setState({ textInput: text });
          }}
          onKeyPress={event => this.handleOnKeyPress(event.nativeEvent.key)}
          onSelectionChange={event =>
            this.setState({ selection: event.nativeEvent.selection })
          }
          {...this.props}
        />
      );
    }
  };
};

export default withMention;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 将本机传递函数作为prop反映到子组件 - React Native pass function to child component as prop 如何将数据从子组件传递到父组件的 state 反应原生? - how to pass data from child component to state of parent component react native? 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 如何从父组件 prop 访问子组件 prop - How to access the child component prop from parent component prop 如何在React Native中从子组件回调到父组件 - How to get callback from child component to parent component in React Native React Native:如何从子组件中的父组件访问变量? - React Native: How to access a variable from parent component in child component? 如何在父项更改时将道具传递给反应组件但不更新子项中的该道具? - How do I pass a prop to a react component yet not update that prop in the child when parent changes? 如何在反应中将对象数组从父组件传递到子组件 - How to pass Array of Objects from Parent Component to Child Component in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM