简体   繁体   English

正确的为什么要将带有参数的函数绑定到React Native中的子组件

[英]Proper why to bind a function with parameters to child component in React Native

I am still new to React Native and I struggle a bit with the programming paradigm. 我仍然是React Native的新手,我对编程范例有点挣扎。 What I am trying to do (by following the structure of another React.js project) is to create a container (parent component) which contains a number of other components. 我想要做的(通过遵循另一个React.js项目的结构)是创建一个容器(父组件),其中包含许多其他组件。 My ultimate goal is to pass and handle all of the props in the parent component. 我的最终目标是传递和处理父组件中的所有道具。 Child component only shows them. 子组件仅显示它们。 My structure looks something like this: 我的结构看起来像这样:

export default class TemporaryCardRequestScreen extends React.Component {
    constructor(props) {
        super(props);
        this.showDateTimePicker = this.showDateTimePicker.bind(this);
        this.hideDateTimePicker = this.hideDateTimePicker.bind(this);
        this.handleDatePicked = this.handleDatePicked.bind(this);

        this.state.fromDateTime = {
            isVisible: false,
            value: new Date()
        }
    }

    showDateTimePicker = () => { /*body*/ };

    hideDateTimePicker = () => { /*body*/ };

    handleDatePicked = date => { /*body*/ };

    render() {
        return (
            <DateTimePickerComponent 
                isVisible={this.state.fromDateTime.isVisible}
                onConfirmPressed={this.handleDatePicked}
                onCancelPressed={this.hideDateTimePicker}
                showDateTimePicker={this.showDateTimePicker}
                value={this.state.fromDateTime.value}
            />
        );
    }
}

and the, my child component looks something like this: 而且,我的子组件看起来像这样:

// npm ref.: https://github.com/mmazzarolo/react-native-modal-datetime-picker
export default class DateTimePickerComponent extends Component {
    constructor(props) {
      super(props);
    }

    render() {
        const {  
            isVisible,
            onConfirmPressed,
            onCancelPressed,
            showDateTimePicker } = this.props;

        return (
            <>
            <Button title="Show DatePicker" onPress={showDateTimePicker} />
            <DateTimePicker
                isVisible={isVisible}
                onConfirm={onConfirmPressed}
                onCancel={onCancelPressed}
                mode='datetime'
                is24Hour={false}
                date={new Date()}
            />
        </>
      );
    }
  }

My focus now is on 我现在的重点是

onConfirmPressed={this.handleDatePicked}

currently, this.handleDatePicked accepts a single argument but I'd like it to accept one additional which is passed to it at the place being used in child component. 目前, this.handleDatePicked接受一个参数,但我希望它接受一个在child组件中使用的地方传递给它的附加参数。

So, my ultimate goal would be to have something similar to this: 所以,我的最终目标是得到类似的东西:

render() {
        const {  
            isVisible,
            onConfirmPressed,
            onCancelPressed,
            showDateTimePicker,
            dateTimePickerId } = this.props;

        return (
            <>
            <Button title="Show DatePicker" onPress={this.showDateTimePicker} />
            <DateTimePicker
                isVisible={isVisible}
                onConfirm={onConfirmPressed(dateTimePickerId)}
                onCancel={onCancelPressed}
                mode='datetime'
                is24Hour={false}
                date={new Date()}
            />
        </>
      );
    }

So, in this way, in my parent component I could have a single method which can handle the updates for a number of date time pickers in my container (This is actually my use-case). 所以,通过这种方式,在我的父组件中,我可以使用一个方法来处理容器中许多日期时间选择器的更新(这实际上是我的用例)。 Instead of having the same type of handlers (with different property name) for pretty much the same thing. 而不是使用相同类型的处理程序(具有不同的属性名称)几乎相同的东西。

UPDATE: Snack expo 更新: 小吃博览会

You can capture 'onConfirm' of DateTimePickerComponent, then call parent function and pass in the dateTimePickerId. 您可以捕获DateTimePickerComponent的'onConfirm',然后调用父函数并传入dateTimePickerId。

TemporaryCardRequestScreen TemporaryCardRequestScreen

// Modify to accept 2 arguments
handleDatePicked = (id, date) => { 
    if (id == "1") {
        // code here
    }
    else if (id == "2") {
        // code here
    } 
};
render() {
    return (
        <div>
            <DateTimePickerComponent 
                isVisible={this.state.fromDateTime.isVisible}
                onConfirmPressed={this.handleDatePicked}
                onCancelPressed={this.hideDateTimePicker}
                showDateTimePicker={this.showDateTimePicker}
                value={this.state.fromDateTime.value}
                dateTimePickerId="1"
            />
            <DateTimePickerComponent 
                isVisible={this.state.fromDateTime.isVisible}
                onConfirmPressed={this.handleDatePicked}
                onCancelPressed={this.hideDateTimePicker}
                showDateTimePicker={this.showDateTimePicker}
                value={this.state.fromDateTime.value}
                dateTimePickerId="2}
            />
        </div>

        );
    }

DateTimePickerComponent DateTimePickerComponent

onMyCustomConfirmPressed = (date) => {
    // Parent onConfirmPressed() shall accept 2 arguments
    this.props.onConfirmPressed(this.props.dateTimePickerId, date)
}

render() {
    const {  
        isVisible,
        onConfirmPressed,
        onCancelPressed,
        showDateTimePicker,
        dateTimePickerId } = this.props;

    return (
        <>
        <Button title="Show DatePicker" onPress={this.showDateTimePicker} />
        <DateTimePicker
            isVisible={isVisible}
            onConfirm={this.onMyCustomConfirmPressed}
            onCancel={onCancelPressed}
            mode='datetime'
            is24Hour={false}
            date={new Date()}
        />
        </>
    );
}

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

相关问题 React Native函数绑定参数 - React Native function bind parameters React Native中如何从子组件调用父函数并将子组件的参数传递给父组件? - How can calling parent function from child component and passing parameters from child component to parent component in React Native? 我在 React 子组件中的 function 的参数没有传递给父组件的 function。为什么会这样? - Parameters of my function in React child component are not being passed to the parent component's function. Why is this happening? 如何从 react-native 中的子组件(带参数)调用父 function? - How do you call a parent function from a child component (with parameters) in react-native? 将函数传递给子组件 react native - pass a function to child component react native 将本机传递函数作为prop反映到子组件 - React Native pass function to child component as prop 如何在 React Native 中调用子组件 function? - How to call child component function in React Native? 为什么我们需要在React Native中绑定函数? - Why we need to bind function in React Native? react-Native:在父组件内部访问子组件函数 - react-Native: Access child component function inside parent component 为什么 props 不能与我的子组件 React JS (react native) 一起使用 - Why props is not working with my child component React JS (react native)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM