我在react-redux应用程序中使用react-datepicker模块,并使其与redux表单兼容,如下所示:
const MyDatePicker = props => (
<div>
<DatePicker
{...props.input}
dateFormat="DD-MM-YYYY"
selected={props.input.value
? moment(props.input.value, 'DD-MM-YYYY')
: null}
placeholderText={props.placeholder}
disabled={props.disabled}
/>
{
props.meta.touched && props.meta.error &&
<span className="error">
{ props.intl.formatMessage({ id: props.meta.error }) }
</span>
}
</div>
);问题是我不知道如何在我的模块中添加一个默认值。此默认值必须为今天。你有什么办法来处理这件事吗?
发布于 2017-04-18 15:01:14
更改:
selected={props.input.value
? moment(props.input.value, 'DD-MM-YYYY')
: null}t0
selected={props.input.value ? moment(props.input.value, 'DD-MM-YYYY') : moment()}
const MyDatePicker = props => (
<div>
<DatePicker
{...props.input}
dateFormat="DD-MM-YYYY"
selected={props.input.value ? moment(props.input.value, 'DD-MM-YYYY') : moment()}
placeholderText={props.placeholder}
disabled={props.disabled}
/>
{
props.meta.touched && props.meta.error &&
<span className="error">
{ props.intl.formatMessage({ id: props.meta.error }) }
</span>
}
</div>
);发布于 2017-04-18 14:58:53
您应该使用moment(dt).format()设置日期的格式
const MyDatePicker = props => {
var date = new Date();
var todayDate = moment(date).format('DD-MM-YYYY');
return (
<div>
<DatePicker
{...props.input}
dateFormat="DD-MM-YYYY"
selected={props.input.value
? moment(props.input.value).format('DD-MM-YYYY')
: todayDate}
placeholderText={props.placeholder}
disabled={props.disabled}
/>
{
props.meta.touched && props.meta.error &&
<span className="error">
{ props.intl.formatMessage({ id: props.meta.error }) }
</span>
}
</div>
);
}发布于 2017-04-18 15:00:53
您可以在mapStateToProps阶段指定初始表单值:
const mapStateToProps = state => {
return {
initialValues: {
date: moment()
} // Use the `initialValues` property to set your initial data
};
}这里也解释了这一点:http://redux-form.com/6.6.3/examples/initializeFromState/
https://stackoverflow.com/questions/43465468
复制相似问题