React state empty when changing route

lianbwl

I have this code:

constructor(props) {
    super(props);
    this.state = {
        setMenu: (window.location.pathname.split("/")[1] == '') ? '/' : window.location.pathname.split("/")[1]
    };

    this.menuItens = new Menu();
    this.menuItens = this.menuItens.Itens;
};

and in my sidebar menu item:

<NavLink tag={Link}
         to={item.path}
         title={item.title}                                                  
         onClick={this.setMenu}                                                  
         className={(this.state.setarMenu === item.title) ? 'active' : ''}>

        <span>{item.nome}</span>
</NavLink>

I'm trying to change the state by calling a function:

setMenu = (event) => {
    this.setState({setarMenu: event.target.title});
};

But unfortunately, sometimes the state doesn't change because the window.location.pathname came empty.

Anybody have a clue why this happens?

lianbwl

Solved, simply remove the tag of the NavLink component of Reactstrap and it works fine.

Appears that, with the span tag inside the NavLink, the component sometimes return only with no attributes like title={item.title} instead of with attributes that I need to "set menu" (setState).

BTW: In PT-BR we write "item" no "iten", that's why sometimes we "misspelled" it.

Thank you guys anyway.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

React state changing when pushing nested object

From Dev

React state changing when pushing nested object

From Dev

How to hide a child component when changing React route

From Dev

Changing the state of a React Component

From Dev

React state is empty after setting

From Dev

React state is empty after setting

From Dev

Empty state in component ( react redux )

From Dev

How should I go about changing the state of siblings when a child is clicked in React?

From Dev

Can't figure out why React state is changing when I am not mutating any of the data or setting state anywhere

From Dev

AngularJS clear model when changing route

From Dev

React js changing state does not update component

From Dev

How to store values in react, that are not state changing?

From Dev

react update screen after changing values in the state

From Dev

changing state before next keydown in react

From Dev

Template empty initially but renders properly on changing and coming back to route

From Dev

Template empty initially but renders properly on changing and coming back to route

From Dev

Route state name in React+Redux

From Dev

Create dynamic React Route components based on state

From Dev

Loading state from parent route component in react

From Dev

Maintain Activity state when changing to a different Activity

From Dev

Maintaining State When Changing Routes in AngularjS

From Dev

Switch button changing state when scrolling in listview

From Dev

State not changing when unit testing VueJS and VueResource

From Dev

Controller is not getting call when its state is changing

From Dev

qcheckbox not changing checked state when clicked on

From Dev

UISwitch is changing state on other cell when scroll

From Dev

Show/hide subview when changing button state

From Dev

Change state's array without changing the whole state (REACT / REDUX)

From Dev

State value not updating when passed as route prop

Related Related

  1. 1

    React state changing when pushing nested object

  2. 2

    React state changing when pushing nested object

  3. 3

    How to hide a child component when changing React route

  4. 4

    Changing the state of a React Component

  5. 5

    React state is empty after setting

  6. 6

    React state is empty after setting

  7. 7

    Empty state in component ( react redux )

  8. 8

    How should I go about changing the state of siblings when a child is clicked in React?

  9. 9

    Can't figure out why React state is changing when I am not mutating any of the data or setting state anywhere

  10. 10

    AngularJS clear model when changing route

  11. 11

    React js changing state does not update component

  12. 12

    How to store values in react, that are not state changing?

  13. 13

    react update screen after changing values in the state

  14. 14

    changing state before next keydown in react

  15. 15

    Template empty initially but renders properly on changing and coming back to route

  16. 16

    Template empty initially but renders properly on changing and coming back to route

  17. 17

    Route state name in React+Redux

  18. 18

    Create dynamic React Route components based on state

  19. 19

    Loading state from parent route component in react

  20. 20

    Maintain Activity state when changing to a different Activity

  21. 21

    Maintaining State When Changing Routes in AngularjS

  22. 22

    Switch button changing state when scrolling in listview

  23. 23

    State not changing when unit testing VueJS and VueResource

  24. 24

    Controller is not getting call when its state is changing

  25. 25

    qcheckbox not changing checked state when clicked on

  26. 26

    UISwitch is changing state on other cell when scroll

  27. 27

    Show/hide subview when changing button state

  28. 28

    Change state's array without changing the whole state (REACT / REDUX)

  29. 29

    State value not updating when passed as route prop

HotTag

Archive