score:1
Ah, ok if you wanna keep it relatively similar, let me suggest a possible simplification for ya:
export interface IListAgenciesProps {
agencyType: string;
agencyTypeTitle: string;
}
const ListAgencies = (props: any) => {
const agencyType = props.match.params["AgencyType"];
const ctx = useContext(imsStore);
const { agencies, loadAgencies } = ctx;
useEffect(() => {
loadAgencies(agencyType);
}, [agencyType]);
let html: JSX.Element;
if (agencies.length > 0) {
html = ( <Container>
<Table celled selectable>
<Table.Header>
<Table.Row>
<Table.HeaderCell>Agency Name</Table.HeaderCell>
<Table.HeaderCell>Telephone</Table.HeaderCell>
<Table.HeaderCell>Email</Table.HeaderCell>
<Table.HeaderCell>Is Active?</Table.HeaderCell>
<Table.HeaderCell>Hourly Rate</Table.HeaderCell>
</Table.Row>
</Table.Header>
<Table.Body>
{agencies.map(a => {
return (
<Table.Row>
<Table.Cell>{a.name}</Table.Cell>
<Table.Cell>{a.telephone}</Table.Cell>
<Table.Cell>{a.email}</Table.Cell>
<Table.Cell>{a.isActive}</Table.Cell>
<Table.Cell>{a.pricePerHour}</Table.Cell>
</Table.Row>
);
})}
</Table.Body>
</Table>
</Container>
);
} else {
html = ( <Container>
<Header as='h1'>
Sorry, we couldn't find the agency type you're looking for.
</Header>
</Container>
);
}
return <>{html}</>;
};
export default observer(ListAgencies);
Store:
@observable agencies: IAgency[] = [];
@action loadAgencies = async (agencyType) => {
try {
this.agencies = await agent.Agencies.list(agencyType);
} catch (err) {
console.log(err);
} finally {
}
};
So what's happening now is we're simplifying the component so it saves the agencyType
on mount. Then specifying it in the deps array for the effect means that we call loadAgencies
everytime the params change. Notice we're now calling the ctx
method with a parameter. So loadAgencies
is gonna take in that type, go fetch the data, and then set it to the context variable all inside the same @action
.
Then, since we've dereferenced agencies
from ctx
inside the component, it will re-render when the agencies
@observable
is updated. We don't need to make the call inside the effect async - the component will re-render whenever the data comes back.
Source: stackoverflow.com
Related Query
- Reload react component after updating state inside useEffect
- React useEffect doesn't reload my component after state update
- React Child Component Not Updating After Parent State Change
- React component table is being rendered multiple times or duplicated after updating state
- Too many re-renders. React limits the number of renders to prevent an infinite loop. Updating state of a functional component inside the render method
- Component state not updating after unchecking checkbox in React
- React 16.8 hooks - child component won't re-render after updating parent state
- Updating React child component after state change
- React Hooks: using useState inside useEffect doesn't set the state immediately after first rendering
- Updating component state inside of useEffect
- React updating state after component has been unmounted (memory leak)
- Updating component state by Listening to socket events inside react hooks or handling them inside middleware? what are the downsides of each one?
- React state still undefined after fetching inside useEffect
- React update state after updating a different one in useEffect
- React Component Not Being Populated even after updating state and executing forEach
- How to call useEffect after updating state in a component
- React Component not updating even after duplication of state in Redux reducer
- State of React child doesn't update after updating parent component
- Can't stop React from updating the main component after state change
- React component using previous state after updating
- React component isn't rendering after updating state by useState
- React Hooks changing state of an array inside of asycn function after useEffect call
- React : Updating and then accessing state after a network request in useEffect hook. State remains stale
- Array in state not updating after useEffect runs in React
- socket connect event inside useEffect does not work when component is created but works after refreshing the page in React
- How to test a function call after state receive value inside expression in React function component using Jest
- React UseState is not re-rendering the App Component after updating the state
- React hooks updating component after value inside object inside another object changes - how to?
- state is not updating in child component after updated inside parent component
- Function component doesn't re-render after updating array state in react
More Query from same tag
- Value of property is undefined in reactJS render() method
- Pass prop to different page in Next JS/ React
- How to map through json response in react using options tag?
- Problem with using setTimeout function together with an arrow function that sets a particular state
- extending React.Component class
- React - Focus on an "input" element (rendered as an element of a list) in the DOM vanishes after keypress
- *React-table* second page not showing data
- React. Component declaration using fat arrow function (ES6 syntax specification)
- Apollo Mutation does not recognize variables
- What are the benefits of immutability?
- React compile error using "create-react-app"
- Persistent Warning: "Each child in an array or iterator should have a unique 'key' prop" even if key is already set
- Hashrouter always redirecting to index
- Jest mock callback function from a promise
- Can't call setState on a component that is not yet mounted, even though it is?
- React Mixed content error while loading bundle.js (Http/ Https)
- Webpack cannot find module
- Create-react-app don't run even just after create it
- Duplicate pathname issue in useLocation
- Passing data from one component to another in React using formik
- redux saga deleting functionality
- How to dismiss a React Modal using createPortal()?
- Do I *have to* flatten all hierarchical component declarations in React.JS?
- How can I copy to clipboard and preserve formatting
- Google App Engine/ Custom Domain - Preferring https over http
- How to have React re-render a functional component when prop changes
- updating an array in state
- Relay QueryRenderer does not return expected props from query
- How to implement UI based on nested array?
- How to write type annotation for higher-order component?