react-native - 将 redux 表单 input.value 绑定到 datetimepicker
问题描述
我有一个 react native/redux/redux 表单设置,它(大部分)工作,所以一切都连接起来并运行。
当我设置一个异步更新的表单时,我得到一个日期字段,我为此定义了一个自定义组件,以<Field>
在 redux-form 中使用。
这是我的组件:
import React, { Component } from "react";
import { TouchableOpacity, StyleSheet } from "react-native";
import { Text, theme, Button, Block, Input } from "galio-framework";
import DateTimePicker from "react-native-modal-datetime-picker";
export class DateInputField extends Component {
constructor(props) {
super(props);
this.state = { isDateTimePickerVisible: false };
this.handleChange = this.handleChange.bind(this);
}
showDateTimePicker = () => {
this.setState({ isDateTimePickerVisible: true });
};
hideDateTimePicker = () => {
this.setState({ isDateTimePickerVisible: false });
};
handleChange = date => {
console.log("date->" + date);
this.setState({ isDateTimePickerVisible: false, date: date });
this.props.input.onChange(date);
};
render() {
const { input, meta, ...inputProps } = this.props;
return (
<Block style={styles.container}>
<TouchableOpacity onPress={this.showDateTimePicker}>
<DateTimePicker
date={new Date(input.value)}
cancelTextIOS="Annulla"
confirmTextIOS="Conferma"
isVisible={this.state.isDateTimePickerVisible}
onConfirm={this.handleChange}
onCancel={() => this.setState({ isDateTimePickerVisible: false })}
/>
<Input
color={"black"}
editable={false}
// onTouch={this.showDateTimePicker}
// onFocus={this.showDateTimePicker}
label={this.props.label}
style={styles.input}
placeholder={this.props.placeholder}
value={
this.state.date != undefined
? this.state.date.getDate() +
"/" +
(this.state.date.getMonth() + 1) +
"/" +
this.state.date.getFullYear()
: "gg/mm/aaaa"
}
/>
</TouchableOpacity>
</Block>
);
}
}
export const styles = StyleSheet.create({
container: {},
label: {},
input: { flex: 1, color: "red", padding: 0, height: 50 }
});
和我的领域:
<Field name={"consignment_date"} label="Fine" component={DateInputField} />
该组件有效,当我按下该字段时,会datepicker
显示来自与其连接的“模型”字段的正确日期。
现在我的问题是,当人工“onChange”事件未更新字段但更新了组件的状态(随后呈现了组件)时,我无法找到一种优雅的方式来更新字段值。我尝试了许多设置状态date
字段的组合,但我总是以无限循环结束,因为更新状态会导致渲染等等。
我尝试了许多组件生命周期事件来读取input.value
并设置它在 state.displayedDate 属性上,但我想我错过了一个非常明显的方法来做到这一点,因为我对 React 的动态知识知之甚少。
非常感谢任何帮助。
解决方案
发布有效的解决方案:
import React, { Component } from "react";
import { TouchableOpacity, StyleSheet } from "react-native";
import { Text, theme, Button, Block, Input } from "galio-framework";
import DateTimePicker from "react-native-modal-datetime-picker";
export class DateInputField extends Component {
constructor(props) {
super(props);
this.state = { isDateTimePickerVisible: false }; //no more date in state
this.handleChange = this.handleChange.bind(this);
}
showDateTimePicker = () => {
this.setState({ isDateTimePickerVisible: true });
};
hideDateTimePicker = () => {
this.setState({ isDateTimePickerVisible: false });
};
handleChange = date => {
this.setState({ isDateTimePickerVisible: false });
this.props.input.onChange(date);
};
render() {
const { input, meta, ...inputProps } = this.props;
return (
<Block style={styles.container}>
<TouchableOpacity onPress={this.showDateTimePicker}>
<DateTimePicker
style={styles.label}
date={new Date(input.value)}//date is transformed from input
onDateChange={this.handleChange}
cancelTextIOS="Annulla"
confirmTextIOS="Conferma"
isVisible={this.state.isDateTimePickerVisible}
onConfirm={this.handleChange}
onCancel={() => this.setState({ isDateTimePickerVisible: false })}
/>
<Input
color={"gray"}
editable={false}
enabled={false}
label={this.props.label}
style={styles.input}
placeholder={this.props.placeholder}
value={ //value is transformed to a date, then to the local string representation
input.value !== ""
? new Date(input.value).toLocaleDateString()
: new Date().toLocaleDateString()
}
/>
</TouchableOpacity>
</Block>
);
}
}
export const styles = StyleSheet.create({
container: {},
input: {
flex: 1,
color: "red",
height: 50,
borderWidth: 0,
borderBottomWidth: 1
}
});
推荐阅读
- c++ - 无法识别的模板声明错误 - 不明白我在做什么错
- android - 使用数据绑定的问题:val vs var 和使用 invalidateAll()
- javascript - 当我在这个数组上使用 forEach 时,如何从数组中删除一个项目?
- mobile - 我如何摆脱这个 itemBuilder 和 forEach 问题?
- elixir - 减少一张地图中的地图列表
- python - 如何使用 python 列出 Azure 虚拟机?
- python - 在python中将值从一个函数传递到另一个函数
- python - 使用 Python 分解电子邮件地址
- python-3.x - 标准正态分布在 NumPy 和 PyTorch 的实践中是如何工作的?
- vb.net - 如何修复图像的“找不到资源”