Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react redux importing reducer dispatch

I’m trying to follow the counter example in the react-redux template, but I can’t seem to export my own setOwner reducer.

ownerSelector.tsx:

export  const ownerSlice = createSlice({
  name: 'owner',
  initialState,

  reducers:{
    setOwner: (state) => {    //Export this reducer
      let own = document.getElementById("OwnerSelector") as HTMLSelectElement
      state.id = parseInt(own!!.options[own.selectedIndex].value);
    }
  },
  extraReducers: (builder) => {}
})

export const selectOwner= (state: RootState) => state.selectedOwner.id;
export default ownerSlice.reducer;

where I want to import setOwner in carForm.tsx:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import {setOwner} from './ownerSelector'; // error here


    let submitHandler = (e: React.FormEvent<HTMLElement>) => {
        e.preventDefault();

        dispatch(setOwner())
        const formData = {
            car: {
                year: year,
                make: make,
                model: model,
                price: price,
                person_id: owner,
            }
        }
        dispatch(createCarAsync(formData));
        resetState();
    }

The error at the import is: Module '"./ownerSelector"' has no exported member 'setOwner'. Did you mean to use 'import setOwner from "./ownerSelector"' instead?ts(2614)

What am I doing wrong? Thanks.

>Solution :

In redux toolkit and in any redux you need to dispatch actions.

export const { setOwner } = ownerSlice .actions 
//now you can use it in dispatch 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading