Reputation: 2157
I am attempting to track the value of two login input fields by getting their value on change and storing this value in the local state so that when the user submits the login, I can just get the current values from the state. However, this results in the inputs flickering when I type text into them and also a weird glitch when I type in the password field (the cursor jumps back to the email field). The glitch can be observed below (note, I am not shift-tabbing back to the email field, it seems to be happening automatically for some reason when I type in the password field).
The following is the code for my profile_tile.js
which I have as a component nested within a navbar:
import React, { useState } from 'react'
import { useFirebase, isLoaded, isEmpty } from 'react-redux-firebase'
import { useSelector } from 'react-redux'
import Modal from '../../modal'
export default function ProfileTile() {
const firebase = useFirebase()
const auth = useSelector(state => state.firebase.auth)
const profile = useSelector(state => state.firebase.profile)
const [showLoginModal, setLoginModalShow] = useState(false)
const [showSignupModal, setSignupModalShow] = useState(false)
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const createNewUser = () => {
firebase.createUser({ email, password })
setLoginModalShow(false)
setSignupModalShow(false)
}
const handleEmailChange = event => {
setEmail(event.target.value)
console.log('EMAIL' + event.target.value)
}
const handlePasswordChange = event => {
setPassword(event.target.value)
console.log('PASSWORD' + event.target.value)
}
const logout = () => {
firebase.logout()
}
const handleLoginModalShow = () => {
setLoginModalShow(true)
setSignupModalShow(false)
}
const handleLoginModalHide = () => {
setLoginModalShow(false)
}
const handleSignupModalShow = () => {
setLoginModalShow(false)
setSignupModalShow(true)
}
const handleSignupModalHide = () => {
setSignupModalShow(false)
}
const handleLogin = () => {
firebase.login({ email: email, password: password })
setLoginModalShow(false)
setSignupModalShow(false)
}
const loginModal = showLoginModal ? (
<Modal>
<div class='vh-100 dt w-100 bg-black-80 overlay fixed z-999'>
<div class='v-mid dtc tc center '>
<article class='mw6 center br2 bg-white ba b--black-10'>
<svg
viewBox='0 0 12 12'
height='20'
width='20'
version='1.1'
onClick={handleLoginModalHide}
class='hand dim fr pr3 pt3'
>
<line
x1='1'
y1='10'
x2='10'
y2='1'
stroke='black'
stroke-width='1'
/>
<line
x1='1'
y1='1'
x2='10'
y2='10'
stroke='black'
stroke-width='1'
/>
</svg>
<main class='black-80'>
<form class='measure center mt3 mb4'>
<fieldset id='sign_up' class='ba b--transparent mh0'>
<div class='mt4'>
<input
class='ph2 pv3 input-reset ba b--black-10 w-100'
type='email'
name='email-address'
id='email-address'
placeholder='Email'
onChange={handleEmailChange}
/>
</div>
<div class='mv4'>
<input
class='ph2 pv3 input-reset ba b--black-10 w-100'
type='password'
name='password'
id='password'
placeholder='Password'
onChange={event => setPassword(event.target.value)}
/>
</div>
<a
href='#0'
class='tc f6 fw6 b link dim br2 ph3 pv3 mb2 dib white bg-blue w-100'
onClick={handleLogin}
>
Sign In
</a>
</fieldset>
<div class='lh-copy mv3'>
<a href='#0' class='f6 link dim black db'>
Forgot your password?
</a>
</div>
<hr class='bb mv3 b--black-10' />
<div class='pt2 flex justify-center'>
<span class='flex b mr2'>Don't have an account?</span>
<a
href='#0'
class='b link dim blue db'
onClick={handleSignupModalShow}
>
Sign Up
</a>
</div>
</form>
</main>
</article>
</div>
</div>
</Modal>
) : null
const signupModal = showSignupModal ? (
<Modal>
<div class='vh-100 dt w-100 bg-black-80 overlay fixed z-999'>
<div class='v-mid dtc tc center '>
<article class='mw6 center br2 bg-white ba b--black-10'>
<svg
viewBox='0 0 12 12'
height='20'
width='20'
version='1.1'
onClick={handleSignupModalHide}
class='hand dim fr pr3 pt3'
>
<line
x1='1'
y1='10'
x2='10'
y2='1'
stroke='black'
stroke-width='1'
/>
<line
x1='1'
y1='1'
x2='10'
y2='10'
stroke='black'
stroke-width='1'
/>
</svg>
<main class='black-80'>
<form class='measure center mt3 mb4'>
<fieldset id='sign_up' class='ba b--transparent mh0'>
<div class='mt4'>
<input
class='ph2 pv3 input-reset ba b--black-10 w-100'
type='email'
name='email-address'
id='email-address'
placeholder='Email'
onChange={handleEmailChange}
/>
</div>
<div class='mv4'>
<input
class='ph2 pv3 input-reset ba b--black-10 w-100'
type='password'
name='password'
id='password'
placeholder='Password'
onChange={handlePasswordChange}
/>
</div>
<a
href='#0'
class='tc f6 fw6 b link dim br2 ph3 pv3 mb2 dib white bg-blue w-100'
onClick={createNewUser}
>
Sign Up
</a>
</fieldset>
</form>
</main>
</article>
</div>
</div>
</Modal>
) : null
return (
<div>
<div class='flex items-center'>
{!isLoaded(auth) ? (
<a class='noselect br2 f6 fw6 dib blue mr2 bg-white ba b--blue no-underline pv3 ph4'>
loading
</a>
) : isEmpty(auth) ? (
<a
class='noselect br2 f6 fw6 dib mr2 bg-white blue grow no-underline pv3 ph4'
onClick={handleLoginModalShow}
>
Log In
</a>
) : (
<div class='flex'>
<span class='noselect br2 f6 fw6 dib white mr2 bg-purple no-underline pv3 ph4'>
<div>{profile.email}</div>
</span>
<a
class='noselect br2 f6 fw6 dib white mr2 bg-green grow no-underline pv3 ph4'
onClick={logout}
>
Log Out
</a>
</div>
)}
</div>
{loginModal}
{signupModal}
</div>
)
}
My modal.js
component is as follows:
import React, { useState, useEffect } from 'react'
import ReactDOM from 'react-dom'
import './style.css'
const modalRoot = document.getElementById('modal-root')
const modalElement = document.createElement('div')
export default function Modal(props) {
useEffect(() => {
modalRoot.appendChild(modalElement)
return () => {
modalRoot.removeChild(modalElement)
}
})
return ReactDOM.createPortal(props.children, modalElement)
}
My inputs are situated within a modal component which I display as an overlay using a portal. I have gone through related questions and tried using other methods but nothing seems to work. I appreciate any help!
Upvotes: 3
Views: 6045
Reputation: 203051
Your useEffect
hook doesn't have a dependency array which will run on every render. You likely meant to append a child once when mounting so try an empty dependency array.
export default function Modal(props) {
useEffect(() => {
modalRoot.appendChild(modalElement)
return () => {
modalRoot.removeChild(modalElement)
}
}, []); // Empty dependency array to run once "on mount"
return ReactDOM.createPortal(props.children, modalElement);
}
Upvotes: 4