Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

lose ref value after rerendering page #133

Closed
k-hoang opened this issue Aug 6, 2019 · 3 comments
Closed

lose ref value after rerendering page #133

k-hoang opened this issue Aug 6, 2019 · 3 comments

Comments

@k-hoang
Copy link

k-hoang commented Aug 6, 2019

https://codesandbox.io/s/recursing-mendeleev-mcbb3
Before re-rendering, click focus button. It should work. It will focus the textarea.

However, if you click the re-render button, this will cause state to update and the app to rerender and after this, if you click the focus button, it will not focus because the ref is null.

@bkremenovic
Copy link

https://codesandbox.io/s/recursing-mendeleev-mcbb3
Before re-rendering, click focus button. It should work. It will focus the textarea.

However, if you click the re-render button, this will cause state to update and the app to rerender and after this, if you click the focus button, it will not focus because the ref is null.

I have the same problem. InnerRef just doesn't work

@bkremenovic
Copy link

https://codesandbox.io/s/recursing-mendeleev-mcbb3
Before re-rendering, click focus button. It should work. It will focus the textarea.

However, if you click the re-render button, this will cause state to update and the app to rerender and after this, if you click the focus button, it will not focus because the ref is null.

Solved ! :) Use it like this:

constructor(props) {
    super(props);
    this.textarea = React.createRef();
}

render() {
    return (
        <TextareaAutosize rows={1} maxRows={6} ref={this.textarea} />
    );
}

Then, the element can always be accessed using this.textarea.current

@k-hoang
Copy link
Author

k-hoang commented Aug 7, 2019

@k-hoang k-hoang closed this as completed Aug 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants