0

I have done the api call this way, I am able to see the return array in network, it has a single array with store details, I have to output the store name, how to render it, I am familiar with map function to display lista. How to render a single output

import StoreService from "../../services/Store/store.service";
import { Store } from "../../models/Store/store.model";
    
    interface OfferListProps {
        activeStoreId: string;
    }
    
    function Overview(props: OfferListProps) {
        const { activeStoreId } = props;
        const [loading, setLoading] = useState(false);
        const [stores, setStores] = useState<Store[]>([]);
    
        const handleFetchStore = () => {
            StoreService.fetchStores(
                (store: Store[]) => {
                    setStores(stores);
                },
                () => { },
                () => { }
            );
        };
    
        useEffect(() => {
            if (activeStoreId) {
                handleFetchStore();
            }
        }, [activeStoreId]);

enter image description here

6
  • Why do you need to fetch all stores StoreService.fetchStores for a single store display? Commented Mar 29, 2022 at 6:02
  • can you tell me the real way to do it ? Commented Mar 29, 2022 at 6:56
  • you can check StoreService to find something like fetchStore (not fetchStores) that would help you get a single store by store id. If it's not there, I can help you out with a workaround Commented Mar 29, 2022 at 7:01
  • I have showStore in StoreService Commented Mar 29, 2022 at 7:07
  • static showStore( storeId: string, onSuccess: Function, onError: Function, onFinal: () => void ) { const API_URL = ApiRoutes.STORES_URL + '/' + storeId; return axiosInstance .get(API_URL) .then(response => { const store = deserialize(Store, response.data["store"]); onSuccess(store); }) .catch(error => { onError(error); }) .finally(() => { onFinal(); }) } Commented Mar 29, 2022 at 7:10

1 Answer 1

1

Instead of using an array in the state, you can pass an object for a single store rendering

const [store, setStore] = useState<Store>();
import StoreService from "../../services/Store/store.service";
import { Store } from "../../models/Store/store.model";
    
    interface OfferListProps {
        activeStoreId: string;
    }
    
    function Overview(props: OfferListProps) {
        const { activeStoreId } = props;
        const [loading, setLoading] = useState(false);
        const [store, setStore] = useState<Store>(); //only set a single store
    
        const handleFetchStore = (storeId) => {
            StoreService.showStore(storeId,
                (store: Store) => {
                    setStore(store); //set store data
                },
                () => { },
                () => { }
            );
        };
    
        useEffect(() => {
            if (activeStoreId) {
                handleFetchStore(activeStoreId);
            }
        }, [activeStoreId]);

After this, you can use store for rendering

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.