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.

Return an event just as any input.

See original GitHub issue

I am making a form, with multiple fields, all my fields send a prop to the parent component, just like this: ChildComponent

function NameInput (props) {  
  return (
    <React.Fragment>
      <label htmlFor="username">Nombre de Usuario</label>
      <input
        className="form-control"
        id="username"
        name="username"
        type="text"
        placeholder="Enter username"
        value={props.data}
        onChange={props.handleChange}
        />    
    </React.Fragment>
  )
};

The on change method, in the parent element, call action and add the new property to my state, with redux, and all the crazy things…

ParentComponent

handleChange = event => {
  const {name, value} = event.target
  this.props.addField({...this.props.user, [name] : value})
}

It works fine with any type of field, like names, email, etc etc… But this workflow doesn’t work with react-DatePicker, this is my current approach:

function birthInput(props) {  
  return (
  <React.Fragment>
    <label htmlFor="username">Año de nacimiento</label>
    <DatePicker 
      name="nacimiento" 
      selected={props.data} 
      onChange={props.handleChange} />
  </React.Fragment>
  )
};

But, checking out what I get in my on change method, and found a simple date: Wed Mar 20 2019 16:00:39 GMT+0100 (hora estándar de Europa central)

Expected behaviour

Return the event from the input field.

Actual behavior

a dateObject

Steps to reproduce

Follow the example.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:10

github_iconTop GitHub Comments

5reactions
lodab111commented, Feb 19, 2020

I came across this issue of not being able to get the data i needed from the event of the input. I am creating each input dynamically and I am reading the name of the event.target to decide where to update my state. I found that I was able to hack it a bit and just create an object to mock the data I needed from the event.target object.

DatePicker = (fieldData) => {
return (
  <DatePicker 
  selected={fieldData.Value}  
  isClearable
  className="DatePickerTextArea"
  onChange={(value, event) => {
	event.target = {type:"text", value:value, name:fieldData.Name}
	fieldData.ChangeHandler(event)
  }}
   />
);
}
4reactions
linkerxcommented, Mar 23, 2019

@josuevalrob las one is a good solution if you only have one date, i want to dynamically add from several datepickers, and its imposible without event target. Solution: one handler for every datepicker

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLElement: input event - Web APIs | MDN
The input event fires when the value of an , , or element has been changed.
Read more >
Events: change, input, cut, copy, paste
The input event triggers every time after a value is modified by the user. ... It's possible to copy/paste not just text, but...
Read more >
Event when input value is changed by JavaScript?
Any time you change $input.value in code, call the code you want ... A simple way is just to trigger an input event...
Read more >
Handling Events :: Eloquent JavaScript
Event listeners are called only when the event happens in the context of the object they are registered on. <button>Click me</button> <p>No handler...
Read more >
The before and after Events - JustPy
Even if you have not assigned an input handler to that particular element, you may want to update the value anyway so it...
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