0
Hello,
This is my first project with React Js and I have a question, I imagine, basic.
I have code below that is working perfectly:
import React, { Component } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import './Header.scss';
import logo from '../../assets/images/logo.png';
import { faUserCircle } from '@fortawesome/free-regular-svg-icons';
export default class Header extends Component {
render() {
function handleBurgerBtn() {
const mobile = document.getElementById("mobile");
const navigationWindow = document.getElementById("navigationWindow");
const loginWindow = document.getElementById("loginWindow");
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
navigationWindow.style.display = 'block';
loginWindow.style.display = 'none';
mobile.classList.toggle("eventNavigationWindow");
}
function handleLoginBtn() {
const mobile = document.getElementById("mobile");
const navigationWindow = document.getElementById("navigationWindow");
const loginWindow = document.getElementById("loginWindow");
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
navigationWindow.style.display = 'none';
loginWindow.style.display = 'block';
mobile.classList.toggle("eventLoginWindow");
};
return (
<header>
<nav>
<div id="burgerBtn" onClick={ () => handleBurgerBtn() }></div>
<div id="loginBtn"><FontAwesomeIcon icon={ faUserCircle } onClick={ ()=> handleLoginBtn() } /></div>
</nav>
<div id="top">
<div class="row">
<div class="col-xs-8 col-xs-offset-2 col-md-6 col-md-offset-3">
<img src={ logo } alt="" />
</div>
</div>
<div id="localisation">
<p class="text">Você está em</p>
<p class="city">Botucatu <i class="fas fa-chevron-down"></i></p>
</div>
</div>
<div id="searchBar">
<div class="row">
<div class="col-xs-12">
<form>
<input
type="text"
placeholder="O que: ex: pizzaria, drogaria, loja, etc."
class="form-control searchField"
/>
</form>
</div>
</div>
</div>
</header>
);
}
}
The question is this: I have two functions and both have the same code snippet:
const mobile = document.getElementById("mobile");
const navigationWindow = document.getElementById("navigationWindow");
const loginWindow = document.getElementById("loginWindow");
I wonder if you have any way for me to use, declare these constants before the reuse functions in the two functions.
I thought about using state but could not implement. Could someone give me a help?
Boy, didn’t work gives error: Typeerror: Cannot read Property 'style' of Undefined. I checked via console.log the return of the navigationWindow constant within the function at the moment of clicking, return null
– Pedro Meneghel
In your example code I have no elements with mobile "id", navigationWindow and loginWindow. The elements I have identified with id are loginBtn, top, localisation and Searchbar
– Bins