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

TypeError: this.setState is not a function #82

Open
anisharya16 opened this issue Jul 20, 2020 · 6 comments
Open

TypeError: this.setState is not a function #82

anisharya16 opened this issue Jul 20, 2020 · 6 comments

Comments

@anisharya16
Copy link

Is this a bug report, a feature request, or a question?
Bug

Is the bug reproducible in a production environment (not a debug one)?
Yes

Environment
"react": "16.11.0",
"react-native": "0.62.2",
"react-native-responsive-screen": "^1.4.1",

Target Platform:
Android (9.0)

Bug:
TypeError: this.setState is not a function. (In 'this.setState({orientation: screenWidth < screenHeight ? 'portrait' : 'landscape'})', 'this.setState' is undefined)

I was using class component but had to change it to functional component as per project requirement. Since I changed it to functional component, orientation change is not working and am getting this error. I have attached screenshot for reference to this. Need solution for this, as i have used this library many times in my project.

WhatsApp Image 2020-07-20 at 12 00 28

@anisharya16
Copy link
Author

Any solution for this issue?

@gregfenton
Copy link

Is the code where you are calling this a functional component or a class component?

Can you show us the code where you are calling listenOrientationChange() ?

@gregfenton
Copy link

gregfenton commented Aug 7, 2020

I suspect your issue is related to PR #70

@gregfenton
Copy link

See the PR I just pushed: PR #83

It has instructions on how to download and use it. Hopefully we can get this pulled into master and published soon.

@samimhakimi
Copy link

Hello,
you have changed from class component to functional component thus, this.setState is now working in Functional component instead you can use Hooks.

@tejfaster
Copy link

make some changes in code like :

const Orientation = () => {
const [dimensions, setDimensions] = useState({ window, screen });

useEffect(() => {
    const subscription = Dimensions.addEventListener(
        'change',
        ({ window, screen }) => {
            setDimensions({ window, screen });
        }
    );
    return () => subscription?.remove();
});
return dimensions.screen.height > dimensions.screen.width ? 'portrait' : 'landscape'

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants