i am new to react , just understanding the concept on redux without using redux thunk. please see the below code
// app.js
import React, { Component } from 'react';
import {connect} from 'react-redux';
import * as actions from './actions'
class App extends Component {
render() {
return (
<div>
<button onClick={this.props.fetchData}>Show Data</button>
</div>
);
}
}
const mapStateToProps = state => {
return {
}
}
const mapDispatchToProps = dispatch => {
return {
fetchData: () => dispatch(actions.fetchDataHandler)
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
// index.js
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import {createStore} from 'redux';
import Data from './reducers';
import {Provider} from 'react-redux';
const store = createStore(Data)
ReactDOM.render(<Provider store={store}><App /></Provider>, document.getElementById('root'));
registerServiceWorker();
//actions-index.js
export const fetchDataHandler = e => dispatch => {
console.log("hit here");
}
//reducers-index.js
// default state
const defaultState = {
data: null
}
let data;
export default data = (state=defaultState, action) => {
switch(action.type){
case "FETCH_DATA":
return {
...state,
data: action.payload
}
default:
return{
...state
}
}
}
folder structure is
src
actions
index.js
reducers
index.js
app.js
i am not using redux thunk, when the button is clicked it will call the fetchData which will invoke the actions.fetchDataHandler
so on the console it should get a message as "hit here", but its not working.
sorry if i am not understanding the redux concept properly.