简体   繁体   中英

Why after onClick my state is undefined ? [Hook, React]

I'm trying to integrate hooks into an old one page React application. For beginin i just replace the parent component (class) by a function component.

  • I've 3 sections on my one page application.
  • I have a menu at the top of the page with the 4 sections and which allows an automatic scroll to the target section.

Problem : when i click on an item menu, i've "Uncaught TypeError: Cannot read property '1' of undefined"


1 - When my application is mounting i call firebase and set datas on a state :

export const App = () => {

const [datas, setDatas] = useState([]);

// FIREBASE ------------------------------------ //
useEffect(() => {
    const datas = base.syncState('/data', {
      context: {
        setState: ({ datas }) => setDatas({ ...datas }),
        state: { datas },
      },
      state: 'datas'
    })

    return () => {
      base.removeBinding(datas);
    }
  }, []) 

// USED TO MANAGE THE ACTIVE SECTION ----------- //
const toggleClassActive = (key) => {
    Object.keys(datas[0].itemsMenu).map(key => datas[0].itemsMenu[key].active = false)
    datas[0].itemsMenu[key].active = true
    setDatas(datas[0].itemsMenu)
}

return (
    <div className="container">
        <Menu itemsMenuAction={toggleClassActive}></Menu>
        <Section id="section1" sectionObj={datas[0].sections[1] ? datas[0].sections[1] : "" } />
        <Section id="section2" sectionObj={datas[0].sections[2] ? datas[0].sections[2] : "" } />
        <Section id="section3" sectionObj={datas[0].sections[3] ? datas[0].sections[3] : "" } />
    </div>
)

2- <Menu> component code is a class, I haven't changed it by a function component yet. I just changed the parent component.

class Menu extends Component {

    render() {

        const { itemsMenuAction, --otherProps--} = this.props

        return (
          <div className="main-menu">
            <ul className={openClassState ? 'list-item-menu open' : 'list-item-menu'}>
              {
                Object.keys(itemsMenuState)
                .map(key =>
                  <li className={itemsMenuState[key].active ? 'item-menu active' : 'item-menu'}
                    onClick={ () => itemsMenuAction(key)}
                    key={key}>
                    {itemsMenuState[key].label+"."}
                  </li>
                )
              }

            </ul>
            <div className={openClassState ? "btn-menu open" : "btn-menu"} onClick={openMenuAction}>
              <nav>
                <a href="#" className="menu">
                  <div className="menu--text">
                    <span data-hover>Menu</span>
                    <span data-close>Fermer</span>
                  </div>
                </a>
              </nav>
            </div>
          </div>
        )
    }
}
}

export default Menu

3 - My json datas :

{
  "data" : [{
    "itemsMenu" : [ {
      "active" : false,
      "label" : "Accueil"
    }, {
      "active" : true,
      "label" : "Services"
    }, {
      "active" : false,
      "label" : "Savoir-Faire"
    }, {
      "active" : false,
      "label" : "Localisation"
    }, {
      "active" : false,
      "label" : "contact"
    } ],
    "sections" : [ {
      "admin" : {
        "content" : "Section 0 - pette",
        "title" : "Services"
      },
      "tech" : {
        "class" : "section-0",
        "parallax" : false
      }
    }, {
      "admin" : {
        "content" : "Section 1 - TEST",
        "title" : "Section 1 - Titre"
      },
      "tech" : {
        "class" : "section-1",
        "parallax" : false
      }
    }, {
      "admin" : {
        "content" : "Section 2 - Content",
        "title" : "Section 2 - Titre"
      },
      "tech" : {
        "class" : "section-2",
        "parallax" : false
      }
    }, {
      "admin" : {
        "content" : "Section 3 - Content",
        "title" : "Section 3 - Titre"
      },
      "tech" : {
        "class" : "section-3",
        "parallax" : false
      }
    } ]
  }]
}


Error screenshot :

enter image description here

Try:

<div className="container">
        <Menu itemsMenuAction={toggleClassActive}></Menu>
        <Section id="section1" sectionObj={datas.length ? datas[0].sections[1] : "" } />
        <Section id="section2" sectionObj={datas.length  ? datas[0].sections[2] : "" } />
        <Section id="section3" sectionObj={datas.length  ? datas[0].sections[3] : "" } />
    </div>

It was a stupid error :

const toggleClassActive = (key) => {
    Object.keys(datas[0].itemsMenu).map(key => datas[0].itemsMenu[key].active = false)
    datas[0].itemsMenu[key].active = true
    setDatas(datas[0].itemsMenu)
}

--> setDatas(datas) !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM