[英]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.