-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Input fields lose their focus on change #15
Comments
import * as React from 'react'
import { useAutofocusRef } from 'react-transition-context'
const Page = ({ page }) => {
// this is just an example, you could also manage field values with redux or a form state library
const [email, setEmail] = React.useState("")
const handleChange = React.useCallback((e) => setEmail(e.target.value), [
setEmail,
])
const autofocusRef = useAutofocusRef()
return (
<div className="page">
This is view {page}
<input
type="text"
name="email"
value={email}
onChange={handleChange}
ref={autoocusRef}
/>
</div>
)
} and then later render it like this, it should focus the input. <SimpleViewSlider
className="outer"
viewStyle={{ height: "100%" }}
viewportStyle={{ height: "100%" }}
innerViewWrapperStyle={{ height: "100%" }}
>
<Page key={page} page={page} />
</SimpleViewSlider> |
Thanks for the quick reply. Actually I tried using
Is there a way to use |
You should pass the global state and callback down to the input, I was just showing an example with local state. But that isn't actually what's causing the input to lose focus. Are you saying that for example page 1 and page 2 both have email fields and you want the email field on page 2 to be focused after you transition to it from page 1? |
No, I got that it is done using Here's a code sandbox that demonstrates my issue: https://codesandbox.io/s/summer-star-zso9k?file=/src/App.js Just for info, if you were to implement a state in your demo so that at the last click, the user has all the form values in an object, what would be the approach? |
Thanks for the code sandbox, now I can see the problem:
Well, I was just trying to make the example simple. I don't actually use local state for form values in my projects, I use If you want to use either of those libraries they have examples of how to implement this type of form:
|
Here's an example of what I mean about different function instances each time: function App() {
const EmailForm = () => {}
return EmailForm
}
const emailFormA = App()
const emailFormB = App()
console.log(emailFormA === emailFormB) // false Unfortunately, even though the code in When you render |
Oh I understand it now! Thanks a lot for taking the time to explain, and providing relevant links. Great library btw! |
Thanks! Hope it goes well implementing the form, to me forms still feel more tedious to implement than they should be, at least with |
I agree compared to Angular and Vue for example..they are indeed more tedious |
I'm using the below code in my
App.js
:In
{content}
, I have an input text field<input type="text" name="email" value={email} onChange={setForm}/>
The issue is, that
<div className="page">
is rerendered on change ofpage
every time and that means the inputs lose focus as they are changed.I'm building a multistep form, pretty much followed the code here, without Material UI. But there doesn't seem to be any input binding to state in the code
(useState)
How can I ensure the input fields stay in focus?
The text was updated successfully, but these errors were encountered: