How can I use Pikaday with ReactJS?

空扰寡人 提交于 2021-02-07 08:16:45

问题


I would like to use the Pikaday datepicker with ReactJS. It is not based on ReactJS. How can I use it?


回答1:


To use Pikaday with ReactJS you can require it as you would any other node module. The trick is to hook into the React rendering event so that after the view is rendered, Pikaday is initialized on any rendered DOM inputs you want to use it on. You can do this with the ReactJS lifesystle event componentDidRender:

  componentDidMount: function() {
    new Pikaday({
      field: React.findDOMNode(this.refs.start),
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeStart
    });

    new Pikaday({
      field: React.findDOMNode(this.refs.end),
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeEnd
    });
  }



回答2:


To get this working in React 16.3 I had to update what you had to the below.

import React from 'react'
import Pikaday from 'pikaday'

class PikadayWrap extends React.Component {
  constructor (params) {
    super(params)
    this.myRef = React.createRef()
  }

  componentDidMount () {
    new Pikaday({
      field: this.myRef.current,
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeStart
    })
  }

  render () {
    return <div>
      <input type='text' ref={this.myRef} />
    </div>
  }
}
export default PikadayWrap

See this question for more info.

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React



来源:https://stackoverflow.com/questions/30058477/how-can-i-use-pikaday-with-reactjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!