0

I am using a countBadge component which is called in parent and assigned to a variable; It is passed to the child; In child it will show badge based on viewType If viewType is fullView, count badge will be shown else countBadge will not be shown;

But countBadge component is called only when the viewType is changed to fullView;

When it is changed to fullView then only the component is called and api for count inside it is called; Each time when the viewType is changed to fullView, API for count is called;

  const countBadge = <CountBadgeWithListener id="SID" url={'submenu'} />

 return (

                 <SideMenuWithSubMenu
                    icon={<SubmenuIcon />}
                    headerText="Submenu"
                    subMenuItems={[
                      {
                        to: '/submenu',
                        text: 'Inbox',
                        notification: countBadge
                      }
                    ]}
                  />
  )

In the child component notification prop is displayed

             {viewType=== 'fullView' && <Box sx={{ pr: 3 }}>{item.notification}</Box> }

I dont want to mount the countBadge component each time viewType is changed.

4
  • <Component /> this is not calling. This creates component instance. Calling happens when the Virtual DOM uses that instance, it must be included in the return of the Component. İf it is not used, its body never gets executed. You can not change that behavior. You can hide it via css, or if the state is the problem, lift it to the parent. Commented Mar 20, 2024 at 5:46
  • Thanks. Actually inside CountBadgeWithListener component I have a API call to get the count. So I dont want to run everyTime when the viewType is changed. Is there any better way to do this.? Commented Mar 20, 2024 at 5:52
  • As I said, hide it via css - this way you will call it only once. Or lift the state logic into the parent Commented Mar 20, 2024 at 8:01
  • @Oktay Yuzcan Thanks for the information. Lifting state up worked. Commented Mar 20, 2024 at 10:10

0

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.