Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

216
Vistas
constructor resetting the value automatically

I am using the internal setState function to change the colour of my link in navbar.

But due to some reason the color does not change in first click but does change in second click. I think the constructor function is updating the state automatically.

I know that as soon as the setState function is called the components are rerendered automatically but the activeIndex remains still '0' after clicking the "ALL DATA" link which instead should become '1' as we can see from the code.

Content of Nav.js:-

import React from 'react';
import { Link } from 'react-router-dom';

import LogOutLink from './LogOutLink';
import './Nav.scss';

class MyLink extends React.Component {
    handleClick = () => this.props.onClick(this.props.index)

    render() {
        return <Link to={this.props.to} className={this.props.isActive ? 'link active' : 'link'} onClick={this.handleClick}>{this.props.text}</Link>
    }
}


class Nav extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            activeIndex: 0
        };
    }

    handleClick = (index) => {
        this.setState({
            activeIndex: index
        })
    }


    render() {
        return (
            <>
                <ul>
                    <li><MyLink to='/search' index={0} isActive={this.state.activeIndex === 0} onClick={this.handleClick} text='SEARCH' /></li>
                    <li><MyLink to='/alldata' index={1} isActive={this.state.activeIndex === 1} onClick={this.handleClick} text='ALL DATA' /></li>
                    <div style={{ float: 'right' }}><li><LogOutLink /></li></div>
                </ul>
            </>
        );
    }
}

export default Nav;

Content of Nav.scss:-

li{
    display: inline;
}

.link{
    display: inline;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 25px;
}

.link:hover:not(.active){
    background-color: grey;
}

.active{
    background-color: white;
    color: black;
}

Screenshot

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I got the solution.

Content of Nav.js:-

import React, { useState } from 'react';
import { Link } from 'react-router-dom';

import LogOutLink from './LogOutLink';
import './Nav.scss';

class MyLink extends React.Component {
    handleClick = () => this.props.onClick(this.props.index)

    render() {
        return <Link to={this.props.to} className={this.props.isActive ? 'link active' : 'link'} onClick={this.handleClick}>{this.props.text}</Link>
    }
}


let activeIndex = 0;

const Nav = () => {

    const handleClick = (index) => {
        activeIndex = index;
    }

    return (
        <>
            <ul>
                <li><MyLink to='/search' index={0} isActive={activeIndex === 0} onClick={handleClick} text='SEARCH' /></li>
                <li><MyLink to='/alldata' index={1} isActive={activeIndex === 1} onClick={handleClick} text='ALL DATA' /></li>
                <div style={{ float: 'right' }}><li><LogOutLink /></li></div>
            </ul>
        </>
    );
}

export default Nav;
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda