M
M
mr jeery2018-02-10 00:23:00
JavaScript
mr jeery, 2018-02-10 00:23:00

Is there a nice and simple redux + mongodb example?

From working examples and more or less simple ones, I found only
https://github.com/krambertech/spa-webinar
, but here flux, not react
, tried to figure it out, but everything looks very confusing.
maybe someone recently figured out the react + redux + mongodb interaction and can help?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BugsCreator, 2018-02-10
@jeerjmin

You need react-thunk. In the component, you call action, which sends a request to the server api and passes the received response to the action dispatch.
In the component:

import { submit } from './actions'
...
onSubmit = (credentials) =>
  this.props.submit(credentials)
    .then((res) => console.log(res))
    .catch((err) => console.log(err))
...
export default connect(null, { submit })(ComponentName);

In action:
import api from './api';

export const handleSuccessResponse = (data) => ({
  type: USER_LOGGED_IN,
  data
})

export const submit = (credentials) =>
  (dispatch) => api.request(credentials).then(responseData => dispatch(handleSuccessResponse(responseData)));

In api (using axios as an example):
import axios from 'axios';

export default request: (credentials) => axios.post('/api/request', { credentials }).then(res => res.data)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question