5

The State

redux state

The tracing results

redux trace dispatch

Each time when I click the button, the dispatch run twice, like the picture above.

This is the AppBar component and mapDispatchToProps function.

const mapStateToProps = state => {
  return {
    title: state.title
  };
};
const mapDispatchToProps = {
  onClick: () => {
    return {
      type: "TOGGLE_SIDEBAR"
    };
  } 
};

const AppBar = props => (
  <Box>
    <Button icon={<Notification />} onClick={props.onClick} />
  </Box>
);
const AppBatContainer = connect(
  mapStateToProps,
  mapDispatchToProps
)(AppBar);
export default AppBatContainer;

This is the reducer

import {
  TOGGLE_SIDEBAR
} from "../constants/action-types";

const initialState = {
  showBar: false
};

const rootReducer = (state = initialState, action) => {
  switch (action.type) {

    case TOGGLE_SIDEBAR:
      return Object.assign({}, state, {
        showBar: !state.showBar
      });
    default:
      return state;
  }
};

export default rootReducer;

This is the store.js

import { createStore, applyMiddleware, compose } from "redux";
import reduxThunk from "redux-thunk";
import rootReducer from "./reducers/index";

const composeEnhancers =
  typeof window === "object" && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
    ? window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
        // Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
        trace: true,
        traceLimit: 20
      })
    : compose;

const enhancer = composeEnhancers(applyMiddleware(reduxThunk));

const store = createStore(rootReducer, enhancer);

export default store;

All libraries are well imported. I tried remove redux-thunk, it is still the same result.

Thanks in advance.

Ref

tim
  • 1,454
  • 1
  • 25
  • 45
  • 2
    try to add a trace feature in your enhancers. `composeWithDevTools({trace: true, traceLimit: 20})` that will let you know where exactly your action is being fired from. In the trace section in your redux devtools – Prasanna May 28 '19 at 11:13
  • @Prasanna Thank you very much, this is a good idea. I tried, as you can see in the updated question, 2 dispatches, one is from redux-thunk, one is from an anonymous function. – tim May 28 '19 at 20:17
  • Hi @tim, can you share your `redux-thunk` function? – Tomer May 29 '19 at 07:04
  • Hi @jank, thanks, in fact there is no `redux-thunk` functions, I did not do any thing asychronous yet. Regards. – tim May 29 '19 at 09:40
  • Thanks all your help @jank and @Prasanna, finally I figured it out. My reducer function is wrong. I should change it to `showBar: !action.showing` with current prop value `showing` in **payload**, I go through the document of redux, but I still did not get it why, becuase I think it is still a pure function in this way `showBar: !state.showBar`, the state is just a parameter, isn't it? – tim May 29 '19 at 09:47
  • are you sure that with that change it only fires once? you may get the result you intended with that change and still fire twice – Tiago Coelho May 29 '19 at 09:52
  • @TiagoCoelho yes, I am sure, it only fire one. [screenshot](https://i.imgur.com/76cj0dd.jpg) – tim May 29 '19 at 20:24

0 Answers0