React: How to access data in all component in react from one view?

I have been writing repeated code to do the same thing on diffrent page, for exmaple: i am trying to access the order list count in diffrent page, i keep writing the same block of code everytime on diffrent compoenent to do the same thing, how to do write this code in one Component and access it in all component, does this have something to do with Context?

    const [orders, setOrders] = useState([])

    useEffect(() => {
        const fetchOrderLists = async () => {
          try {
            const response = await api.get(baseUrl + "/my-order-list/" + user_id + '/');
            setOrders(response.data);
          } catch (error) {
            console.log(error);
          }
        };
        fetchOrderLists();
    }, []);

I have to write this in all pages before i can use orders.length