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.

Prefferable way to Override/append onChange event on Field

See original GitHub issue

Hi, first thanks for this amazing component!

I am struggeling a bit with how to best append/override the onChange event on the Field component. What is the best practices here? I am still quite new to react and very new to redux so still trying to grasp the redux way of thinking, please keep this in mind of my question is trivial.

So, to put it very simple, I am having 2 selects where one will dispatch an action fetching new data through ajax, which the other select will render. I therefore need to pass some extra stuff happening to the onChange event for one of the dropdowns.

I’ve spent quite some hours investigating best approach for this but found older stuff mostly (this for instance; #624). Prefferable I would like to use the Field component doing this as its quite neat.

So far I’ve come up with this solution;

<Field
  name="category"
  component="select"
  className="form-control"
  disabled={this.props.categories.length === 0}
  props={{
    onChange: (e) => {
      this.props.change('category', e.target.value)
      this.props.handleCategoryChange(e)
    }
  }}
>
  <option value="">Select Category</option>
  {this.props.categories.map(option => <option value={option.id} key={option.id}>{option.name}    </option>)}
</Field>

The handleCategoryChange function is passed from the containerComponent of the form and dispatches the required action. This seems to work as intended, but as I am very new to redux and to redux-form I would appreciate to know if there is a better way dealing with this?

Feedback and input on this would be very appreciated.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
Limesscommented, Feb 14, 2017

I believe this was fixed by https://github.com/erikras/redux-form/pull/2385 (v6.5.0), you can now pass onChange as a prop to the <Field /> and it will be called on the change event prior to the redux-form input.onChange prop.

e.g

const Input = ({input: {onChange}}) => (
    <input onChange={onChange} />
);
...
const MyForm = () => (
    <Field onChange={myOnChange} component={Input} />
);

The order of calls will be myOnChange -> input.onChange.

1reaction
gustavohenkecommented, Oct 10, 2017

Hi @ntomallen, yes, it should, unless you call evt.preventDefault()

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the best way to trigger onchange event in react js
The input logic in React now dedupe's change events so they don't fire more than once per value. It listens for both browser...
Read more >
Is there any way to add event handler like onchange on a field
Hi,. I have a form with a textbox and I would like to trigger a Javascript function onchange of the value of this...
Read more >
How to override onchange old api vs new - Odoo
Old api way was to return value dictionary for changed fields, whereas new api way is to assign changes directly to fields of...
Read more >
magento 1.9 - Something is overriding my onchange script?
Hi. I'm not quite sure on how to use your example? I've tried: $( document ).ready(function() { $('#bundle-option-132').bind('change', function ...
Read more >
Backbone.js
Instead, views listen to the model "change" events, ... If your models are located somewhere else, override this method with the correct logic....
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