reactjs - react 日期选择器中的默认值

标签 reactjs datepicker default-value redux-form

我在我的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>
);

问题是我不知道如何在我的模块中添加默认值。该默认值必须是今天。关于如何处理这个问题有什么想法吗?

最佳答案

更改:

 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>
);

关于reactjs - react 日期选择器中的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43465468/

相关文章:

c# - jquery datepicker 在用户更改月份时更改年份

javascript - 在 jQuery UI Datepicker 的输入中显示完整日期

twitter-bootstrap - React 传单未正确渲染

javascript - 无法使用react-form以编程方式设置文本

reactjs - 使用绝对路径导入 react 组件

javascript - 我想禁用整个星期天

reactjs - 在渲染不同的组件时 react "Cannot update a component (` BrowserRouter`)“使用 firebase 注册新用户时发出警告

swift - 获取错误的默认值

php - 使用 DEFAULT 值准备 MySQL INSERT/UPDATE 语句

c# - 在 C# 中设置对象的默认值 `String`