How come the changes in my react component's CSS are not updating in real-time?

After integrating redux into my create-react-app project, I've been struggling to implement a navigator that highlights the active "page link." To achieve this functionality, I am using a combination of react hooks (to maintain the current page state) and the npm package called classNames.

The code snippet I'm utilizing for this purpose is:

classNames(object['key'] && classes.activeItem)

In this setup, when object['key'] evaluates to true for a specific item activation, the corresponding element receives the activeItem class.

Interestingly, when I replace object['key'] with simply true, it functions as expected. Additionally, upon clicking the item, logging the value of object['key'] also returns true.

However, despite these observations, the mechanism seems to be malfunctioning. Any insights on why this might be happening would be greatly appreciated!

import React, { useEffect, memo } from 'react';
import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
...

Answer №1

Remember that the function passed to the useEffect hook always runs after rendering.

Your useEffect does not trigger a re-render for the component to reflect changes. Only changes to state can cause a re-render. If you want a re-render, you should first use the useState hook to update the state and then call setState within the useEffect hook. Alternatively, you could include these actions directly in the render cycle (outside of the useEffect hook):

activePage = _.mapValues(activePage, () => false);
activePage[curPage] = true
useEffect(() => {
    if(!curPage){
      onPageChange('search');
    }
});

Upon reviewing your code, it appears you can simplify by using

curPage === 'invite' && classes.itemActiveItem
instead of
activePage['invite'] && classes.itemActiveItem
, removing the unnecessary lines related to the activePage object. This will streamline things for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with proper mapping of Formik fields and validation using yup on the sign in form is causing a

I've been struggling to implement a basic sign-in form using Formik and Material UI. Despite entering data into the form fields, they remain blank and the validation doesn't seem to be working as expected. import React from 'react'; ...

A distinct handler function designed for a dynamically generated form

I have 3 MaterialUI TextFields that are rendered n number of times based on user input (stored in a variable named groupMembersCount) in a functional ReactJS component using the useState hook: const [groupDetails, setGroupDetails] = React.useState([ { ...

:first-child that does not have the class .disabled

Does anyone have a solution for selecting the initial item in a list that does not possess the "disabled" class? ...

Is there a way to randomly change the colors of divs for a variable amount of time?

I have a unique idea for creating a dynamic four-square box that changes colors at random every time a button is clicked. The twist is, I want the colors to cycle randomly for up to 5 seconds before 3 out of 4 squares turn black and one square stops on a r ...

Error: Definition Already Exists

I'm currently debugging a layout and encountering some peculiar errors. The page is being served as DTD XHTML 1.0 Strict. The error message looks like this: ID "OFFICENAME" already defined: div class="office" id="officename" ID "OFFICENAME" origin ...

Determining the height of the tab bar across devices using React-Navigation

How can I position a component right above the TabBar in React-Navigation V2's createBottomTabNavigator? The height of the TabBar appears to vary on different devices, particularly iOS devices. Is there a method to determine and calculate the actual ...

Tips for effectively testing a DetailPanel-Property within a React Material Table using Jest

The image above showcases a MaterialTable containing various information and offering the ability to execute (1) actions as well as expand a detailed panel to view more information. The code snippet below functions flawlessly within the application: <Ma ...

MUI Group Selector - Array of Arrays

Struggling to create a customized MUI select group using API data. const data = [ { id: '1', name: 'Category 1', services: [ { id: '1', name: 'Service 1', }, ...

The JSON file containing API data is stored within the _next folder, making it easily accessible to anyone without the need for security measures or a login in the Next

When accessing the protected user Listing page, we utilize SSR to call the api and retrieve all user records which are then rendered. However, if one were to check the Network tab in Chrome or Firefox, a JSON file containing all user data is generated and ...

What is the best way to track and document the specific Context Provider being utilized while invoking useContext?

After creating a custom component that displays AuthContext.Provider with specific values, I encountered an issue. Even though it functions correctly, when I utilize useContext within a child of the AuthProvider component, my code editor (VS Code) is unabl ...

What could be causing my component to fail to load properly with Vite?

I have been working on a React project using Vite. Following a tutorial I discovered in an article at https://www.digitalocean.com/community/tutorials/how-to-set-up-a-react-project-with-vite, I encountered an issue. Despite following the tutorial step by ...

-=:Heading with a decorative vertical line=:-

Currently, I am working on a CSS project that requires page titles (headings) to be centered with horizontal lines positioned vertically on both sides. Additionally, there is a background image on the page which means the title's background must be tr ...

CSS shrink effect upon hovering

I'm currently working on designing a menu using <ul>/<li> tags along with CSS styles. Here's the progress I've made so far with my menu design: https://jsfiddle.net/gANfS/6/ However, I encountered an issue where if you hover ov ...

What is the best way to target specific elements within my array using Jquery?

I have a Wordpress website where the posts contain images with the .aligncenter class. I want to style the images that are less than 400px in width by adding the display:inline-block; property and reducing their size to 40%. Here is the approach I tried: ...

Can you designate a specific Google font for each language on a webpage?

Is there a way to use two different Google fonts, one for English characters and another for Thai characters, on a single page? While it's possible to achieve this using the @font-face syntax by specifying unicode character ranges, Google fonts do no ...

Modifying the background image of div elements with JQuery in a loop function is ineffective when using Google Chrome

I am facing an issue in my application where I have a for loop to change the background image of two divs. The code snippet is as follows: for (var i = 0; i < length; i++) { $("div_" + (i + 1)).css("background-image", "../imageFile.png"); ...

I am looking for guidance on removing the bottom line from the ionic 4 segment indicator. Any advice or tips on

.segment-button-indicator { -ms-flex-item-align: end; align-self: flex-end; width: 100%; height: 2px; background-color: var(--indicator-color); opacity: 1; } I am a beginner in hybrid app development and ...

CSS from the parent page is not being applied to AJAX-injected content

I recently added AJAX functionality to a new website to dynamically load and insert Wordpress page content using AJAX. However, I encountered an issue where some CSS styles that were supposed to be loaded in the page head were not being applied. This resu ...

There seems to be an issue with webpack as it is currently unable to find any loaders configured to handle this file in pdfjs-dist when trying to access the new URL("./build

After trying multiple versions of pdfjs-dist": "^3.5.141" to integrate the library in my project, I am still encountering the same error: ./node_modules/pdfjs-dist/webpack.js Module parse failed: Unexpected token (22:43) You may need an appr ...

What is the best way to maintain the highlighting of a clicked navigation button in CSS?

.custom-highlight { display: inline; font-weight: 500; font-size: 15px; } .item-list { border: 1px solid #b1b8c9; color: $color-grey-light; font-size: 14px; display: inline-block; margin-right: 8px; padding: 5px 20px; border-radius: 4p ...