Hello,
I need some help with Redux. I can’t get my state to update beyond the initial null value.
// data.actions.js import { DataActionTypes } from ‘./data.types’ export const setCurrentData = (data) => ({ type: DataActionTypes.SET_CURRENT_DATA, payload: data })
// data.reducer.js import { DataActionTypes } from ‘./data.types’ const INITAL_STATE = { currentData: null } const dataReducer = (state = INITAL_STATE, action) => { switch(action.type) { case DataActionTypes.SET_CURRENT_DATA: return { currentData: action.payload } default: return state } } export default dataReducer
// data.types.js export const DataActionTypes = { SET_CURRENT_DATA: ‘SET_CURRENT_DATA’ }
// userData.component.jsx const UserData = ({ currentUser }) => { const [userData, setUserData] = useState([ { loaded: false, }, ]); const getData = () => { (async () => { const db = firebase.firestore(); const employeeID = currentUser.employeeID; const lastName = currentUser.lastName; const firstName = currentUser.firstName; try { const userRef = db.collection(‘user-data’); const snapShot = await userRef .where(‘f_EmployeeID’, ‘==’, employeeID) .where(‘f_LastName’, ‘==’, lastName) .where(‘f_FirstName’, ‘==’, firstName) .get(); if (snapShot.empty) { console.log(‘No matching Documents’); setUserData([ { loaded: ‘no-match’, }, ]); } snapShot.forEach((doc) => { setUserData([ { data: doc.data(), loaded: true, }, ]); console.log(‘DATA’, doc.data()) setCurrentData(doc.data()) }); } catch (err) { console.error(‘ERROR: ‘, err); } })(); }; // OMITTING OTHER DATA … // const mapDispatchToProps = (dispatch) => ({ setCurrentData: (data) => dispatch(setCurrentData(data)), });
Any help is greatly appreciated!
submitted by /u/Mustang-22
[link] [comments]