Answer the question
In order to leave comments, you need to log in
How to make a correct ajax request in React Redux?
In general, I'm trying to understand the Flux approach in React in RoR. Advised to learn Redux.
Installed gem 'react-rails'.
Redux installed via npm
package.json - it looks something like this.
{
"version": "0.1.1",
"main": "index.js",
"dependencies": {
"react": "^0.13.3",
"react-redux": "^1.0.1",
"redux": "^1.0.1",
"redux-thunk": "^0.1.0"
},
"devDependencies": {
"babelify": "^6.2.0",
"browserify": "^11.0.1",
"browserify-incremental": "^3.0.1"
}
}
export const LOAD_INFO = 'LOAD_INFO';
export function loadInfo() {
return {
type: 'LOAD_INFO',
info: // ?
}
}
componentDidMount: function() {
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'GET',
success: function(data, textStatus, jqXHR) {
this.setState({
book: data.book,
magazine: data.magazine
});
}.bind(this),
error: function(xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
}
import request from 'axios';
export const LOAD_INFO = 'LOAD_INFO';
export function loadInfo() {
return {
type: 'LOAD_INFO',
info: request.get(Routes.root_path(), {
headers: {
'Accept': 'application/json'
}
})
}
}
import { LOAD_INFO }
from '../actions/action';
export default function info(state = 0, action) {
switch (action.type) {
case LOAD_INFO:
return action;
default:
return state;
}
}
import React, { Component, PropTypes } from 'react';
class Info extends Component {
render() {
const { info } = this.props;
return (
<div>
{ info }
</div>
);
}
}
Info.propTypes = {
info: PropTypes.object.isRequired
};
export default Info;
Answer the question
In order to leave comments, you need to log in
Because ajax requests are asynchronous, then for each such request 3 branches of the development of events are made:
In code it will look something like this
export function loadInfo() {
return dispatch => {
dispatch({
type: 'LOAD_INFO_REQUESTED'
});
request.get(
Routes.root_path(),
{headers: {'Accept': 'application/json'}}
)
.then(result => {
dispatch({
type: 'LOAD_INFO_OK',
info: result.data
})
})
.catch(result => {
dispatch({
type: 'LOAD_INFO_FAIL',
errors: result.statusText
})
})
}
}
const defaultState = { loading: false, info: null, errors: null };
export default function info(state = defaultState, action) {
switch (action.type) {
case LOAD_INFO_REQUESTED:
return { loading: true };
case LOAD_INFO_OK:
return { loading: false, info: action.info, errors: null };
case LOAD_INFO_FAIL:
return { loading: false, info: null, errors: action.errors };
default:
return state;
}
}
@connect(state => ({
info: state.info
}))
class Info extends Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch(loadInfo()) // Вызываем загрузку
}
render() {
const { loading, info, errors } = this.props.info;
if (loadind) { return (<div>Loading</div>) }
if (errors != null) { return (<div>Error!</div>) }
return (
<div>
{ info }
</div>
);
}
}
I made a similar tutorial, where asynchronous requests are also understood. I will be glad if I help - https://www.gitbook.com/book/maxfarseer/redux-cour...
There is an excellent module, axios. Returns promises. Very comfortably
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question