1

I declared a state in baseReducer.ts:

import {PayloadAction} from '~common/declarations/action';

const initialState = {
  test: 'baseReducer'
};

const BaseReducer = (state = initialState, action: PayloadAction<any>): any => {
  switch(action.type) {
    default:
      return state;
  }
}

export default BaseReducer;

Then in my orderReducer:

import {PayloadAction} from '~common/declarations/action';

const initialState = {
  test: 'orderReducer'
};

const OrderReducer = (state = initialState, action: PayloadAction<any>): any => {
  switch(action.type) {
    default:
      return state;
  }
}

export default OrderReducer ;

In my rootReducer:

const RootReducer: Reducer = combineReducers({
  BaseReducer,
  OrderReducer
});

export default RootReducer;

Now in my functional component OrderPage.tsx:

export default function OrderPage() {
  const stringText: string = useSelector(state => state.test)  // HERE
  return (
    <React.Fragment>Order Page</React.Fragment>
  );
}

Here is my question:

How do I define which state to take from? Ideally I want to specify the state from OrderReducer.

When using react Classes, I could do the following:

const mapStateToProps = ({orderReducer}) => {
  return {
    test: orderReducer.test
  }
}

Edit:

I tried the following (separately of course!), but it did not work:

const reducer: string = useSelector(state => state.baseReducer);
const reducer: string = useSelector(state => state.orderReducer);

How do I achieve the same in function components?

2
  • 1
    Function component - not functional component. Nothing functional about functions with state :) Commented Apr 14, 2021 at 10:07
  • I do not think baseReducer exists try const reducer: string = useSelector(state => state.OrderReducer); this is in accordance to how you did combine reducers const RootReducer: Reducer = combineReducers({ BaseReducer, OrderReducer }); Commented Apr 14, 2021 at 10:09

1 Answer 1

3

You need to use correct selector function in useSelector:

const stringText = useSelector(state => state.OrderReducer.test)

Or, if you write like (without using property name shorthand):

const RootReducer: Reducer = combineReducers({
  base: BaseReducer,
  order: OrderReducer
});

Then

const stringText = useSelector(state => state.order.test)

You can further avoid this error by defining RootState type:

export type RootState = ReturnType<typeof store.getState>
// ...
const stringText = useSelector((state: RootState) => state.OrderReducer.test)

Edit:

If you are getting error:

OrderReducer does not exist on DefaultRootState.

Then you should define RootState type because DefaultRootState is just export interface DefaultRootState {}.

Sign up to request clarification or add additional context in comments.

3 Comments

I attempted the first solution, but intellij is claiming that OrderReducer does not exist on DefaultRootState. Is that different from the RootState?
where should i export the type RootState?
Did you mean state object? Store object was created using default method from redux in my App.tsx file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.