React – dependency arrays with context providers

Say I have something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React, { createContext, useState, useContext, ReactNode } from 'react';
interface ChannelProviderProps {
children: ReactNode;
}
const ChannelContext = createContext<{ channelId: string, setChannelId: React.Dispatch<React.SetStateAction<string>> } | null>(null);
export const ChannelProvider = ({ children }: ChannelProviderProps) => {
const [channelId, setChannelId] = useState<string>('Initial');
return (
<ChannelContext.Provider value={{ channelId, setChannelId }}>
{children}
</ChannelContext.Provider>
);
};
export const useChannel = () => {
const context = useContext(ChannelContext);
console.log('useChannel context is: ', context);
if (!context) {
const errorMessage = `Existing context not found.n useChannel must be used within a ChannelProvider`
throw new Error(errorMessage);
}
return context;
};
</code>
<code>import React, { createContext, useState, useContext, ReactNode } from 'react'; interface ChannelProviderProps { children: ReactNode; } const ChannelContext = createContext<{ channelId: string, setChannelId: React.Dispatch<React.SetStateAction<string>> } | null>(null); export const ChannelProvider = ({ children }: ChannelProviderProps) => { const [channelId, setChannelId] = useState<string>('Initial'); return ( <ChannelContext.Provider value={{ channelId, setChannelId }}> {children} </ChannelContext.Provider> ); }; export const useChannel = () => { const context = useContext(ChannelContext); console.log('useChannel context is: ', context); if (!context) { const errorMessage = `Existing context not found.n useChannel must be used within a ChannelProvider` throw new Error(errorMessage); } return context; }; </code>
import React, { createContext, useState, useContext, ReactNode } from 'react';

interface ChannelProviderProps {
    children: ReactNode;
}

const ChannelContext = createContext<{ channelId: string, setChannelId: React.Dispatch<React.SetStateAction<string>> } | null>(null);

export const ChannelProvider = ({ children }: ChannelProviderProps) => {
  const [channelId, setChannelId] = useState<string>('Initial');
  return (
    <ChannelContext.Provider value={{ channelId, setChannelId }}>
      {children}
    </ChannelContext.Provider>
  );
};

export const useChannel = () => {
    const context = useContext(ChannelContext);
    console.log('useChannel context is: ', context);
    if (!context) {
      const errorMessage = `Existing context not found.n useChannel must be used within a ChannelProvider`
      throw new Error(errorMessage);
    }
    return context;
};

And I have Component A, and Component B… both of which have this provider wrapped around them:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <ChannelProvider>
<Component A />
</ChannelProvider>
<ChannelProvider>
<Component B />
</ChannelProvider>
</code>
<code> <ChannelProvider> <Component A /> </ChannelProvider> <ChannelProvider> <Component B /> </ChannelProvider> </code>
        <ChannelProvider>
            <Component A />
        </ChannelProvider>


        <ChannelProvider>
            <Component B />
        </ChannelProvider>

If Component A has this code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { channelId, setChannelId } = useChannel();
useEffect(() => {
setChannelId('SomethingElse')
}, []);
</code>
<code>const { channelId, setChannelId } = useChannel(); useEffect(() => { setChannelId('SomethingElse') }, []); </code>
const { channelId, setChannelId } = useChannel();

useEffect(() => {
    setChannelId('SomethingElse')
}, []);

and Component B has this code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { channelId, setChannelId } = useChannel();
useEffect(() => {
console.log(channelId);
}, [channelId]);
</code>
<code>const { channelId, setChannelId } = useChannel(); useEffect(() => { console.log(channelId); }, [channelId]); </code>
const { channelId, setChannelId } = useChannel();

useEffect(() => {
    console.log(channelId);
}, [channelId]);

If both components are mounted, will Component B print out ‘Initial’, or will it print ‘SomethingElse’?

I’m not sure if I’m grasping this exactly. I can appreciate and understand when dependency arrays rely on the component’s local states… but I’m unsure of ‘remote’ states.

Would Component B need to re-render in order to have awareness of this newly updated channelId?

1

In your setup, Component A and Component B are each wrapped in their own ChannelProvider. This means:

  • Each ChannelProvider has its own separate channelId state.
  • Component A updates its own channelId to 'SomethingElse'.
  • Component B has a different ChannelProvider and retains its channelId as 'Initial'.

Therefore, when Component B logs channelId, it will print ‘Initial’ because the two components do not share the same state.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật