score:1
The dispatch is asynchronous, so you either need to watch for result to be updated in your Redux store with componentDidUpdate
or directly return the result from the reducer.
When you get the result, you can manipulate it and store it in local state to reference in your render. Note that unless you need to reference the result in another component somewhere, then you don't need to store it in Redux, you can handle it all in within the component.
Subscribing to the store with componentDidUpdate
:
componentDidMount() {
this.props.dispatch(
AgmtAction.getAgmtsForCustomer(
this.props.match.params.custID,
this.props.match.params.source,
this.props.token
)
);
}
componentDidUpdate(prevProps) {
if (JSON.stringify(prevProps.Agmts) !== JSON.stringify(this.props.Agmts)) {
// this is the result of the dispatch
console.log(this.props.Agmts);
}
}
Returning the result directly back:
// in your AgmtAction.getAgmtsForCustomer action
export const getAgmtsForCustomer = () => (dispatch, getState) => {
return axios
.get(..........
.then((res) => {
dispatch(..........
return res.data;
})
.catch((err) => {
...
});
};
// in your `AgmtContainer` component
...
componentDidMount() {
this.props.dispatch(
AgmtAction.getAgmtsForCustomer(
this.props.match.params.custID,
this.props.match.params.source,
this.props.token
)
).then((res) => {
// this is the result of the dispatch
console.log(res);
});
}
score:1
In getRowsData function where you are getting error "map is not a function" is due to the data you are getting in this.props.Agmts must be an object type. (Object encloses in curly brackets {}).
You can apply map function only on array not on an object. (Array encloses in square brackets [])
Source: stackoverflow.com
Related Query
- how can I set the reducer's return value to state object in component in react js
- How can I redirect to another component in react and pass the state that I set in the previous component?
- How can I set the default state of a checkbox based on value in firestore in react table
- React - How to use the current state value as a variable so i can set as a index inside an array
- How can I set up state in a React app using arrays in a json object as well as arrays not in the json object?
- With this Class component I geocoded a value but now im unable to set the value as a state in the same component, How can I save the geocoded value?
- How can I change the state of one React component based on the value of its sibling?
- how can I change the value of an object in an array being mapped from state react
- How to TEST async calls made in componentDidMount that set the state of React Component
- useState hook can only set one object at the time and return the other object of the same array to initial state
- How can mock the value of a state and data in my react test
- How do I manage state on a React component that can have state changed from the parent or from events upon it?
- React Final Form. How to set field value from component state
- I can not get the state from react router Link component using useLocation. So how can I pass it?
- How can I properly set a type for `useState`, if I want the piece of state to either be an object or null?
- How to access the latest state value in the functional component in React
- How react functional component can use object variable which declared behind the usage? (include example)
- I am trying to animate an object with css in react and change the animation (timing) dynamically, how can I set the style after animation end?
- how do i update state in react for updating the value of an object inside an array using array.map function
- In React, how can a parent component set state from the response an async fetch function performed in a child component?
- How can I set the state of a component on video end in react?
- How to set state in a React component using this.setState without explicitly passing the field name to update?
- How can I set the state and return JSX with the same function
- How can reset a React child component back to original state after it updates state in the parent
- How to set defaultChecked getting value from state React Functional Component Checkbox (React - Hooks)
- How can I set the value of a Form.Item without a form object in Ant.Design?
- How can I change the value of an object in an array in react redux when the action is called?
- How to pass the return value of a function as a property to a react component
- How to get the return value of parent function to child component in react (Using props)
- How can i can change the value of a variable with if/else to return certain div in react
More Query from same tag
- How to pass an Event Object into a nested function?
- Do variable values in useEffect's cleaning function remain as they were in the last time useEffect ran?
- Printing HMTL elements
- Attach Authorization header for all axios requests
- React: Having issues with using contexts with setting the value in the same file as the context declaration
- React component is making infinite API calls
- React useState hook - set value
- Applying specific theme for react material-table
- Multiple checkbox values handling with ReactJS
- Should I use getState directly from store in redux?
- How to get redux-form FieldArray text field to have readonly with initial values but not when a new field.push is clicked?
- Stop accordion header toggle on button click
- Cypress testing a Material-UI datepicker is not working on Github actions
- TypeScript: Return different function based on type
- Unit-testing React Components using Context Consumers
- Access wrapped component function from HOC
- React typescript passing function as prop and calling from child component
- fetch data using multiple ids from firebase
- Using redux, why can't I import a json file directly to createSlice?
- ReactJS | Getting form values without maintaining states or using getElementByID / refs
- Error on import?
- Get Specific Dates from Array of dates in React Datetime
- Customizing the `No data found` validation message in react-select
- Infinite Loop With Higher Order React Redux Component
- dividing function to a separate file
- How do you create multiple forms on the same page with redux-forms v6?
- TypeError: Cannot read property 'state' of undefined React, state passed to other component is undefined
- react - how to correctly use event.preventDefault? Should it be used on <button> onClick?
- Can variable be passing by function (react js)?
- '/' home nav item color is showing active even changing of NavLink in ReactJs