Nach allem, was ich weiß, muss ich Anfrage in Aktion erstellen erstellen. Wie verwende ich ein Versprechen, um eine Anfrage zu senden? Ich bekomme Daten in Aktion. Dann wird ein neuer Zustand in dem Reduzierer erzeugt. Bind Aktion und Reducer in Verbindung. Aber ich weiß nicht, wie man Versprechen für eine Anfrage verwendet.Wie AJAX-Anfrage in Redux machen
Aktion
import $ from 'jquery';
export const GET_BOOK = 'GET_BOOK';
export default function getBook() {
return {
type: GET_BOOK,
data: $.ajax({
method: "GET",
url: "/api/data",
dataType: "json"
}).success(function(data){
return data;
})
};
}
Reducer
import {GET_BOOK} from '../actions/books';
const booksReducer = (state = initialState, action) => {
switch (action.type) {
case GET_BOOK:
return state;
default:
return state;
}
};
export default booksReducer;
Container Wie Anzeigedaten in Container?
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import getBook from '../actions/books';
import Radium from 'radium';
import {Link} from 'react-router';
function mapStateToProps(state) {
return {
books: state.data.books,
};
}
function mapDispatchToProps(dispatch) {
return {
getBooks:() => dispatch(getBook()),
};
}
@Radium
@connect(mapStateToProps, mapDispatchToProps)
class booksPage extends Component {
static propTypes = {
getBooks: PropTypes.func.isRequired,
books: PropTypes.array.isRequired,
};
render() {
const {books} = this.props;
return (
<div>
<Link to={`/authors`}><MUIButton style="flat">All Authors</MUIButton></Link>
<ul>
{books.map((book, index) =>
<li key={index}>
<Link to={`/book/${book.name}`}><MUIButton style="flat"><div class="mui--text-black mui--text-display4">
"{book.name}"</div></MUIButton></Link>
<Link to={`/author/${book.author}`}><MUIButton style="flat"><div class="mui--text-black mui--text-display4">
{book.author}</div></MUIButton></Link>
</li>
)}
</ul>
</div>
);
}
}
export default booksPage;
danke, aber jetzt bekomme ich Warnung: fehlgeschlagen propType: Erforderliche prop 'Bücher' wurde nicht in' booksPage' angegeben. Überprüfen Sie die Rendermethode 'Connect (booksPage)' .warning @ (program): 45 (Programm): 45 Warnung: getDefaultProps wird nur für klassische React.createClass-Definitionen verwendet. Verwenden Sie stattdessen eine statische Eigenschaft namens 'defaultProps'. –
Haben Sie die 'action.data' in den Status reduziert? –
Object.assign ({}, state, { books: action.data.books, Autoren: action.data.authors }); –