Redux-Toolkit-Query: polling, selectfromReult with selectors

I am using below Redux-Toolkit Query (RTKQ) hooks to get information from one of the API and polling this for every 5 seconds, and selectFromResult to get the desired data.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const selectorData = useMemo(() => {
const emptyValue = {},
nullValue = null;
return createSelector(
[
(data) => data,
(data, error) => (error ? error.status : -1),
],
(data, status) => {
if (status >= 400) return emptyValue;
return data ?? nullValue;
},
);
}, []);
const { interestedData } = useMyApiQuery(
args1,
{
selectFromResult: ({ data, error }) => {
interestedData: selectorData(data, error)
}),
pollingInterval: 5000
}
);
// using interestedData to load the data in UI.
</code>
<code>const selectorData = useMemo(() => { const emptyValue = {}, nullValue = null; return createSelector( [ (data) => data, (data, error) => (error ? error.status : -1), ], (data, status) => { if (status >= 400) return emptyValue; return data ?? nullValue; }, ); }, []); const { interestedData } = useMyApiQuery( args1, { selectFromResult: ({ data, error }) => { interestedData: selectorData(data, error) }), pollingInterval: 5000 } ); // using interestedData to load the data in UI. </code>
const selectorData = useMemo(() => {
  const emptyValue = {},
        nullValue = null;

  return createSelector(
    [
      (data) => data,
      (data, error) => (error ? error.status : -1),
    ],
    (data, status) => {
      if (status >= 400) return emptyValue;
        
      return data ?? nullValue;
    },
  );
}, []);

const { interestedData } = useMyApiQuery(
  args1,
  {
    selectFromResult: ({ data, error }) => {
      interestedData: selectorData(data, error)
    }), 
    pollingInterval: 5000
  }
);
    
// using interestedData to load the data in UI.

Using selectors, so that the component does not re-render every 5s and gets re-rendered when there is any update in the data or error.

Is this the correct way of using the selector?

Is this the correct way of using the selector?

I don’t believe it is correct. Selector functions are (generally) passed the entire state object and select out their specific slices of state and return a computed value.

Here you are simply trying to select from the returned query result, not the entire Redux state. I think it is sufficient to simply use a function to do this work.

Note that a shallow equality check is performed on the overall return
value of selectFromResult to determine whether to force a rerender.
i.e. it will trigger a rerender if any of the returned object values
change reference.

So long as your emptyValue and nullValue references are stable, the selectFromResult function should work similar to useSelector selector functions right out-of-the-box.

Outside the component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const emptyValue = {};
const nullValue = null;
</code>
<code>const emptyValue = {}; const nullValue = null; </code>
const emptyValue = {};
const nullValue = null;

Inside the component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { interestedData } = useMyApiQuery(
args1,
{
selectFromResult: ({ data, error }) => {
interestedData: error?.status >== 400
? emptyValue
: data ?? nullValue
}),
pollingInterval: 5000
}
);
</code>
<code>const { interestedData } = useMyApiQuery( args1, { selectFromResult: ({ data, error }) => { interestedData: error?.status >== 400 ? emptyValue : data ?? nullValue }), pollingInterval: 5000 } ); </code>
const { interestedData } = useMyApiQuery(
  args1,
  {
    selectFromResult: ({ data, error }) => {
      interestedData: error?.status >== 400
        ? emptyValue
        : data ?? nullValue
    }), 
    pollingInterval: 5000
  }
);

0

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

Redux-Toolkit-Query: polling, selectfromReult with selectors

I am using below Redux-Toolkit Query (RTKQ) hooks to get information from one of the API and polling this for every 5 seconds, and selectFromResult to get the desired data.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const selectorData = useMemo(() => {
const emptyValue = {},
nullValue = null;
return createSelector(
[
(data) => data,
(data, error) => (error ? error.status : -1),
],
(data, status) => {
if (status >= 400) return emptyValue;
return data ?? nullValue;
},
);
}, []);
const { interestedData } = useMyApiQuery(
args1,
{
selectFromResult: ({ data, error }) => {
interestedData: selectorData(data, error)
}),
pollingInterval: 5000
}
);
// using interestedData to load the data in UI.
</code>
<code>const selectorData = useMemo(() => { const emptyValue = {}, nullValue = null; return createSelector( [ (data) => data, (data, error) => (error ? error.status : -1), ], (data, status) => { if (status >= 400) return emptyValue; return data ?? nullValue; }, ); }, []); const { interestedData } = useMyApiQuery( args1, { selectFromResult: ({ data, error }) => { interestedData: selectorData(data, error) }), pollingInterval: 5000 } ); // using interestedData to load the data in UI. </code>
const selectorData = useMemo(() => {
  const emptyValue = {},
        nullValue = null;

  return createSelector(
    [
      (data) => data,
      (data, error) => (error ? error.status : -1),
    ],
    (data, status) => {
      if (status >= 400) return emptyValue;
        
      return data ?? nullValue;
    },
  );
}, []);

const { interestedData } = useMyApiQuery(
  args1,
  {
    selectFromResult: ({ data, error }) => {
      interestedData: selectorData(data, error)
    }), 
    pollingInterval: 5000
  }
);
    
// using interestedData to load the data in UI.

Using selectors, so that the component does not re-render every 5s and gets re-rendered when there is any update in the data or error.

Is this the correct way of using the selector?

Is this the correct way of using the selector?

I don’t believe it is correct. Selector functions are (generally) passed the entire state object and select out their specific slices of state and return a computed value.

Here you are simply trying to select from the returned query result, not the entire Redux state. I think it is sufficient to simply use a function to do this work.

Note that a shallow equality check is performed on the overall return
value of selectFromResult to determine whether to force a rerender.
i.e. it will trigger a rerender if any of the returned object values
change reference.

So long as your emptyValue and nullValue references are stable, the selectFromResult function should work similar to useSelector selector functions right out-of-the-box.

Outside the component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const emptyValue = {};
const nullValue = null;
</code>
<code>const emptyValue = {}; const nullValue = null; </code>
const emptyValue = {};
const nullValue = null;

Inside the component:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const { interestedData } = useMyApiQuery(
args1,
{
selectFromResult: ({ data, error }) => {
interestedData: error?.status >== 400
? emptyValue
: data ?? nullValue
}),
pollingInterval: 5000
}
);
</code>
<code>const { interestedData } = useMyApiQuery( args1, { selectFromResult: ({ data, error }) => { interestedData: error?.status >== 400 ? emptyValue : data ?? nullValue }), pollingInterval: 5000 } ); </code>
const { interestedData } = useMyApiQuery(
  args1,
  {
    selectFromResult: ({ data, error }) => {
      interestedData: error?.status >== 400
        ? emptyValue
        : data ?? nullValue
    }), 
    pollingInterval: 5000
  }
);

0

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