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.

Question: Integration with redux-form

See original GitHub issue

Can’t figure out the onSubmit method. Let’s say I have the following form:

const formConfig = { form: 'form', fields: ['name',] }

const stateToProps = (state) => {
  return state;
}

const dispatchToProps = (dispatch) => {
  return {
    onSubmit: ({ name }) => {
      // Should return a promise
    }
  }
}

@reduxForm(formConfig, stateToProps, dispatchToProps)
export class Todos extends React.Component {
  render() {
    const { fields: { name }, error, handleSubmit } = this.props;

    return (
      <form  onSubmit={handleSubmit}>
        <input type="text" {...name} />
        <button type="submit">Create</button>
        {error && <div>{error}</div>}
      </form>
    );
  }
}

From the redux-form docs:

If your onSubmit function returns a promise, the submitting property will be set to true until the promise has been resolved or rejected. If it is rejected with an object matching { field1: ‘error’, field2: ‘error’ } then the submission errors will be added to each field (to the error prop) just like async validation errors are. If there is an error that is not specific to any field, but applicable to the entire form, you may pass that as if it were the error for a field called _error, and it will be given as the error prop.

The saga could look like:

function* createTodo(action) {
   try {
      const todo = yield call(Api.createTodo, action.name);
      yield put({ type: 'CREATE_TODO_SUCCESS', todo });
   } catch (e) {
      yield put({ type: 'CREATE_TODO_FAILURE', reason: e.toString() });
   }
}

function* saga() {
  yield* takeEvery('CREATE_TODO', createTodo);
}

Then:

onSubmit: ({ name }) => {
  return new Promise((resolve, reject) => {
    dispatch({ type: 'CREATE_TODO', name });      
  });
}

How could I intercept the CREATE_TODO_SUCCESS and CREATE_TODO_FAILURE actions in the promise? I’m really stuck here.

/cc @erikras

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:15
  • Comments:31 (1 by maintainers)

github_iconTop GitHub Comments

298reactions
tokenvoltcommented, Mar 2, 2016

@rosendi I’m doing this way. In the form component:

onSubmit: (values) => {
  return new Promise((resolve, reject) => {
    dispatch(someActionCreator({ values, resolve, reject }))
  });
}

In saga:

function* saga() {
  while (true) {
    const { payload: { values, resolve, reject } } = yield take(TYPE)
    // use resolve() or reject() here
  }
}
65reactions
oanylundcommented, Oct 19, 2016

I do not know if these action creators are new or have some downside, but could we not just use the startSubmit and stopSubmit action creators and pass the form name with the submit action?

Example:

function* createEntity(entity, apiFn, formId, newEntity) {
  yield put( entity.request() )
  yield put( startSubmit(formId) )
  const {response, error} = yield call(apiFn, newEntity)
  if(response) {
    yield put( entity.success(response) )
    yield put( reset(formId) )
    yield put( stopSubmit(formId) )
  }
  else {
    yield put( entity.failure(error) )
    // handle and format errors from api
    yield put( stopSubmit(formId, serverValidationErrors) )
  }
}

Quite new to both redux-form and redux-saga, so there could be something i am missing here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit and Integration Testing of React/Redux Forms
Today, we'll talk about testing React forms that are connected with Redux, considering both unit and integration testing.
Read more >
Getting Started - Redux Form
To connect your React form components to your Redux store you'll need the following pieces from the redux-form package: Redux Reducer: formReducer ,;...
Read more >
React Redux form and connect syntax - Stack Overflow
My question is , I can't work out the syntax to have the Redux form also to use the connect statement e.g. connect(select,...
Read more >
Get Started with redux-form - Hashrocket
The redux-form library bills itself as the best way to manage your form ... The error helpfully suggests two ways to address this...
Read more >
Why build your forms with Redux-Form - Medium
... Redux-Form by Erik Rasmussen, but it arose other questions like "It ... And to finish our integration we add a reducer that...
Read more >

github_iconTop Related Medium Post

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 Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found