मैं redux-thunk
का उपयोग एसिंक कार्यों और babel-polyfill
के लिए वादे के लिए कर रहा हूं। मुझे निम्न त्रुटि मिलती है: Error: Actions must be plain objects. Use custom middleware for async actions.
Redux-thunk async क्रियाएं: async क्रियाओं के लिए कस्टम मिडलवेयर का उपयोग करें
मैंने अपने मिडलवेयर में redux-promise
सहित इस समस्या को हल किया। मुझे यकीन नहीं है कि मुझे इस समस्या को हल करने के लिए redux-promise
का उपयोग क्यों करना पड़ा क्योंकि Redux दस्तावेज़ों में सभी उदाहरण babel-polyfill
का उपयोग करते हैं। क्या मुझे redux-promise
का उपयोग जारी रखना चाहिए या मुझे babel-polyfill
के साथ कुछ समस्याएं हो सकती हैं?
babel-polyfill
मेरे ऐप प्रवेश बिंदु में शामिल है:
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './components/App.jsx';
import store from './store.jsx';
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>
, document.querySelector('.container'));
अद्यतन:
तो मैं सिर्फ मामले में जाँच की अगर मैं redux-thunk
स्थापित किया है। यह मेरे पैकेज में है। जेसन।
fetchStates(sessionID) {
this.props.dispatch(fetchStates(sessionID));
}
# I bind this function in component's constructor
this.fetchStates = this.fetchStates.bind(this);
और अंत में, यह:
function receiveStates(json) {
return {
type: RECEIVE_STATES,
states: json.states,
};
}
export function fetchStates(uuid) {
return dispatch =>
fetch(`https://my-api.com/session/${uuid}`)
.then(response => response.json())
.then(json => dispatch(receiveStates(json)));
}
यहाँ कैसे मैं घटक से कार्रवाई कहते हैं: यहाँ मेरी store.js
import thunkMiddleware from 'redux-thunk';
import promise from 'redux-promise'
export default store = createStore(
rootReducer,
defaultState,
applyMiddleware(
thunkMiddleware,
promise
)
);
यहाँ action.js में मेरी async कार्रवाई कर रहा है मेरा reducer है:
function statesReducer(state = null, action) {
switch (action.type) {
case RECEIVE_STATES:
return { ...state, states: action.states };
default:
return state;
}
}
http://fiddle.jshell.net/josephj/e02Lcoee/ – zloctb