admin管理员组文章数量:1026989
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 ponent 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
ponent 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 ponent 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!
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 ponent 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
ponent 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 ponent 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!
Share Improve this question edited Jan 1, 2020 at 2:12 mlz7 asked Jan 1, 2020 at 1:59 mlz7mlz7 2,1573 gold badges28 silver badges55 bronze badges 3- Can you post your entire ponent? When I create a sandbox with the exact code you posted, it works exactly as expected. There must be more going on. – Matthew Moran Commented Jan 1, 2020 at 2:06
- @MatthewMoran sorry, I thought it would be too much if I posted the whole thing. I have updated it with the relevant ponent. – mlz7 Commented Jan 1, 2020 at 2:14
-
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. – Drew Reese Commented Jan 1, 2020 at 2:34
1 Answer
Reset to default 4Your 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);
}
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 ponent 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
ponent 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 ponent 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!
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 ponent 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
ponent 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 ponent 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!
Share Improve this question edited Jan 1, 2020 at 2:12 mlz7 asked Jan 1, 2020 at 1:59 mlz7mlz7 2,1573 gold badges28 silver badges55 bronze badges 3- Can you post your entire ponent? When I create a sandbox with the exact code you posted, it works exactly as expected. There must be more going on. – Matthew Moran Commented Jan 1, 2020 at 2:06
- @MatthewMoran sorry, I thought it would be too much if I posted the whole thing. I have updated it with the relevant ponent. – mlz7 Commented Jan 1, 2020 at 2:14
-
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. – Drew Reese Commented Jan 1, 2020 at 2:34
1 Answer
Reset to default 4Your 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);
}
本文标签: javascriptGetting value of input causing flickering in ReactStack Overflow
版权声明:本文标题:javascript - Getting value of input causing flickering in React - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745659058a2161777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论