3

I am calling API on the onBlur/onChange of input field in Ant.Design(react redux based). on Calling API, It update the props of the component and on update of the props it call render method. in render method it re render the UI and call onBlur/onChange again and it got stuck in endless loop.

I have tried maintaining it in the state, but It is not stopping this recursive behavior.

render () {

const {
      form: { getFieldDecorator, getFieldValue },  payload: { payload },
    } = this.props;

 console.log(" Client details " + JSON.stringify(this.props));

return ( <Form onSubmit={this.handleSubmit} hideRequiredMark style={{ marginTop: 8 }}>
            <FormItem {...formItemLayout} label={<FormattedMessage id="form.seller.code" />}>
              {getFieldDecorator('code', {
                rules: [
                  {
                    required: true,
                    message: formatMessage({ id: 'validation.code.required' }),
                  },
                ],
              })(<Input placeholder={formatMessage({ id: 'form.code.placeholder' })}
              onChange={this.fetchDetails()} 
               />)}
            </FormItem>
</Form>

)

}


 fetchDetails() {
    const { dispatch, form } = this.props;
    const value = this.props.form.getFieldValue('code');

dispatch({
       type: 'form/fetchDetails',
       payload: value,
     });
  }

expected result : just fetch the details once actual : repeated calls to onBlur and render method causing stuck in loop.

1
  • Try with onChange={this.fetchDetails} instead of onChange={this.fetchDetails()} Commented Jul 4, 2019 at 12:29

1 Answer 1

2

You are passing this.fetchDetails() (call to function) to onChange, while you should pass this.fetchDetails (reference to function) to it.

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou so much. It is working as expectedly. Can you elaborate the reason, why it was doing so??
Simply because as render gets triggered it calls fetchDetails (it is a function call, not a function reference), which in turn updates the state triggering a new render.. So that's why you got a loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.