I am new to React. I created a demo application.And I am trying to hide the message on a button click. 1) But the action does not call on button click. 2) And why the console shows as following when console the action inside the reducer.This shows on page reloads. view page
inside reducer
redux.js:30 Object {type: "@@redux/INIT"}
redux.js:31 Object {}
redux.js:29 inside reducer
redux.js:30 Object {type: "@@redux/PROBE_UNKNOWN_ACTION_j.r.h.g.s.q.b.y.b.9"}
redux.js:31 Object {}
redux.js:29 inside reducer
redux.js:30 Object {type: "@@redux/INIT"}
redux.js:31 Object {}
my package.json
{
"name": "react-redux-data-flow",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.5",
"redux": "^3.7.0",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"react-scripts": "1.0.7"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
App.js
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import { initialMessageAction, clickButtonAction } from './redux.js';
import { connect } from 'react-redux';
class App extends Component {
render() {
return (
<div className="App">
<div className="App-header">
</div>
<p className="App-intro">
react-redux-data-flow
</p>
{this.props.message? <div>hi ..<button onClick={ () => this.props.clickButtonAction }>hide message</button></div>: ''}
</div>
);
}
}
// mapStateToProps.........................................................................................................
const mapStateToProps = (state, ownProperty) => ({
message:state.geod
});
//...............................................................................................................................
// DispatchStateToProps.........................................................................................................
const mapDispatchToProps = {
// initialMessageAction,
clickButtonAction
}
// connect to Store...............................................................................................................................
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);
// .....................................................................................................................................................................
redux.js
import React from 'react';
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
//actions.js..............................................................................................
export const initialMessageAction = (geod) => ({
type:'INITIAL_MESSAGE',
geod,
});
export const clickButtonAction = () =>{
console.log('inside click button action');
return {
type:'CLICK_MESSAGE',
}
};
//.........................................................................................................
//reducer.js.........................................................................................................
export const geod = (state ={}, action) => {
console.log('inside reducer');
console.log(action);
console.log(state)
switch(action.type){
case 'INITIAL_MESSAGE':
console.log('inside reducer');
return action.geod;
case 'CLICK_MESSAGE':
return [
...state, {
message: ''
}
]
default:
return state;
}
};
//.........................................................................................................
//reducer.js.........................................................................................................
export const reducers = combineReducers({ geod, });
//.........................................................................................................
//store.js.........................................................................................................
// export const store = createStore(reducers, applyMiddleware(thunk));
//.........................................................................................................
export function configureStore(initialState = {}) {
const store = createStore(
reducers,
initialState,
applyMiddleware(thunk)
)
return store;
};
export const store = configureStore();
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import './index.css';
// Add these imports - Step 1
import { Provider } from 'react-redux';
import { store } from './redux';
// ReactDOM.render(<App />, document.getElementById('root'));
// registerServiceWorker();
// Wrap existing app in Provider - Step 2
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
);