Ich Anruf Ajax versuche in reagieren mit redux-thunk
und axios
.Ich Daten aus JSON-Dateiwie Ajax in Reaktion mit `Redux-Thunk` anrufen?
einfache Art und Weise (wie dies auf den Knopf klicken Anruf) erhalten möchten
axios.get('data.json').then((data)=>{
console.log(data);
})
Aber ich möchte verwenden, redux-thunk
. Mit anderen Worten muss ich abonnieren in der Komponente, die Versand sein wird mit thunk
können wir thunk hier ?? hier ist mein Code https://plnkr.co/edit/bcGI7cHjWVtlaMBil3kj?p=preview
const thunk = ReduxThunk.default;
const abc= (state={},action) => {
console.log('in redux', action.type)
switch(action.type){
case 'GET_DATA':
return dispatch =>{
return axios.get('data.json');
};
default :
return state;
}
}
const {createStore,bindActionCreators ,applyMiddleware } =Redux;
const {Provider,connect} =ReactRedux;
const store = createStore(abc,
applyMiddleware(thunk)
);
class First extends React.Component {
constructor (props){
super(props);
}
getDate(){
this.props.getData();
// axios.get('data.json').then((data)=>{
// console.log(data);
// })
}
render(){
return (
<div>
<button onClick={this.getDate.bind(this)}>GET DATA</button>
</div>
)
}
}
const actions = {
getData:() => {
return {
type: 'GET_DATA',
}
}
};
const AppContainer = connect(
function mapStateToProps(state) {
return {
digit: state
};
},
function mapDispatchToProps(dispatch) {
return bindActionCreators(actions, dispatch);
}
)(First);
ReactDOM.render(
<Provider store={store}>
<AppContainer/>
</Provider>
,document.getElementById('root'))