I am new to react, I created a context, it has a function that updates the dictionary using reducer dispatcher. The function takes an object, this object is then converted to another object, and added to the dictionary.
when I try to print the dictionary immediately after calling the AddToDictionary function, I don't get the updated one. It is like the component is not waiting on the context to finish processing the AddToDictionary. How can I fix this??
import React, {
useContext,
useReducer,
} from "react";
import MapViewContext from "./mapview-context.js";
const mapViewEventContext = React.createContext({
AddFeatureToSelection: (newFeature, layer) => {},
FeatureSelectionTracker: {},
});
export const ViewContextHistoryProvider = (props) => {
const reducerFn = (state, action) => {
if (action.type === "SetSelectedFeaturesDictionary") {
let newArray = {};
newArray = { ...action.selectedFeaturesDictionary };
return {
CurrentFeatureSelection: { ...newArray },
};
}
if (action.type === "RemoveFromSelection") {
let newArray = {};
newArray = { ...state.CurrentFeatureSelection };
let newFtr = action.feature;
let lyr = action.layer;
if (
newArray[lyr.layerId] != undefined &&
newArray[lyr.layerId] != null &&
newArray[lyr.layerId].length != 0
) {
var indexOfFtr = newArray[lyr.layerId].findIndex(
(x) => x.ObjectId === newFtr.attributes["OBJECTID"]
);
if (indexOfFtr >= 0) {
let featuresArray = newArray[lyr.layerId];
featuresArray = featuresArray.splice(indexOfFtr);
newArray[lyr.layerId] = featuresArray;
}
}
return {
CurrentFeatureSelection: { ...newArray },
};
}
if (action.type === "AddToSelection") {
let newArray = {};
newArray = { ...state.CurrentFeatureSelection };
let newFtr = action.newFeature;
let lyr = action.featureLayer;
if (newArray[lyr.layerId] == undefined || newArray[lyr.layerId] == null) {
let ftr = CreateFeatureObject(newFtr, lyr);
newArray[lyr.layerId] = [ftr];
console.log("added feature and layer" + ftr);
} else {
let oid = newFtr.attributes["OBJECTID"];
let exists = newArray[lyr.layerId].filter((x) => x.ObjectId === oid);
if (exists.length == 0) {
let ftr = CreateFeatureObject(newFtr, lyr);
newArray[lyr.layerId].push(ftr);
console.log("pushed feature " + ftr);
}
}
return {
CurrentFeatureSelection: { ...newArray },
};
}
return {
CurrentFeatureSelection: state.CurrentFeatureSelection,
};
}
const [reducerTracker, dispachToolFn] = useReducer(reducerFn, {
CurrentFeatureSelection: {},
});
function CreateFeatureObject(ftr, layer) {
// returns object that gets added to the dictionary
}
const AddFeatureToSelectionHandler = (newFeature, layer) => {
dispachToolFn({
type: "AddToSelection",
newFeature: newFeature,
featureLayer: layer,
});
};
return (
<mapViewEventContext.Provider
value={{
AddFeatureToSelection: AddFeatureToSelectionHandler,
FeatureSelectionTracker: reducerTracker.CurrentFeatureSelection,
}}
>
{props.children}
</mapViewEventContext.Provider>
);
};
export default mapViewEventContext;
The Component calls the contexts method AddFeatureToSelection, then immediately when I try to log the context FeatureSelectionTracker I don't get the updated dictionary. basically the context AddFeatureToSelection method completes execution after I log the FeatureSelectionTracker, eventhough I am calling it first from the component.