Skip to content
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

chore: migrate withKeyaboardState class to function component #27415

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions src/components/withKeyboardState.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable react/no-unused-state */
import React, {forwardRef, createContext} from 'react';
import PropTypes from 'prop-types';
import React, {forwardRef, createContext, useEffect, useState} from 'react';
import {Keyboard} from 'react-native';
import PropTypes from 'prop-types';
import getComponentDisplayName from '../libs/getComponentDisplayName';

const KeyboardStateContext = createContext(null);
Expand All @@ -15,32 +14,24 @@ const keyboardStateProviderPropTypes = {
children: PropTypes.node.isRequired,
};

class KeyboardStateProvider extends React.Component {
constructor(props) {
super(props);

this.state = {
isKeyboardShown: false,
};
}

componentDidMount() {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', () => {
this.setState({isKeyboardShown: true});
function KeyboardStateProvider(props) {
const {children} = props;
const [isKeyboardShown, setIsKeyboardShown] = useState(false);
teneeto marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', () => {
setIsKeyboardShown(true);
});
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
this.setState({isKeyboardShown: false});
const keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', () => {
setIsKeyboardShown(false);
});
}

componentWillUnmount() {
this.keyboardDidShowListener.remove();
this.keyboardDidHideListener.remove();
}
return () => {
keyboardDidShowListener.remove();
keyboardDidHideListener.remove();
};
}, []);

render() {
return <KeyboardStateContext.Provider value={this.state}>{this.props.children}</KeyboardStateContext.Provider>;
}
return <KeyboardStateContext.Provider value={{isKeyboardShown}}>{children}</KeyboardStateContext.Provider>;
}

KeyboardStateProvider.propTypes = keyboardStateProviderPropTypes;
Expand Down
Loading