question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Stateless customInput?

See original GitHub issue

I’m trying to pass my existing Input to <DatePicker /> but it happens to be a stateless component. This seems to create two errors for me.

One is this

warning.js:36Warning: Stateless function components cannot be given refs (See ref "input" in Input created by t). Attempts to access this ref will fail.

The other is this, which seems related to #694

Uncaught TypeError: Cannot read property 'focus' of null
    at t.n.setFocus (react-datepicker.min.js:1)
    at react-datepicker.min.js:1

I’m not sure how best to proceed. Has anyone else encountered this scenario?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:31
  • Comments:17

github_iconTop GitHub Comments

69reactions
jmlivingstoncommented, Sep 24, 2019

Building on @mfalade and @hiranvikas77, a full example:

BTW: Works flawlessly if you want to use Reactstrap. Just import Input from reactstrap/lib/Input, then replace input with Input. Of course the popup styles are a bit different.

import React, { forwardRef, useState } from 'react'
import DatePicker from 'react-datepicker'
import 'react-datepicker/dist/react-datepicker.css'

function Example() {
  const [startDate, setStartDate] = useState(new Date())
  const ref = React.createRef()
  const CustomDateInput = forwardRef(({ onClick, value }, ref) => (
    <input onClick={onClick} value={value} onChange={onClick} ref={ref} />
  ))
  return (
    <DatePicker
      selected={startDate}
      onChange={date => setStartDate(date)}
      customInput={<CustomDateInput ref={ref} />}
    />
  )
}

export default Example
48reactions
mfaladecommented, Nov 19, 2019

You can still use a SFC as your CustomDateInput if you want.
The solution is to use React’s forwardRef.

You can update your ./customDateInput.js file to look like this…

import React, { forwardRef } from 'react';

const CustomDateInput = (props, ref) => {
  // blah blah
};

export default forwardRef(CustomDateInput);

Note that customDateInput cannot have propTypes or defaultProps declarations since it isn’t exactly a React component at this point. It’s merely serving as a render function. (You’d get console warnings if specify propTypes or defaultProps)

Resources:

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get an input's value on a button click in a Stateless ...
Your CustomInput component: import React from "react"; import PropTypes from "prop- ...
Read more >
Stateless Model Evaluation - Vespa Documentation
Stateless Model Evaluation ... Vespa's speciality is evaluating machine-learned models quickly over large numbers of data points. However, it can also be used...
Read more >
Passing data into Angular components with @Input
This custom input binding is created via the @Input() decorator! ... Taking a stateless counter component, we need to tell Angular that we'd ......
Read more >
How can I get an input's value on a button click in a Stateless ...
This approach allows your component to remain stateless and also doesn't require ... import PropTypes from "prop-types"; class CustomInput extends React.
Read more >
Custom input transformation — IPython 1.2.1
Custom input transformation. String based transformations. Stateless transformations; Coroutine transformers; Token-based transformers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found