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

Orientation Changes on imported/external StyleSheet.Create objects #24

Open
pedrosimao opened this issue Mar 13, 2019 · 1 comment
Open

Comments

@pedrosimao
Copy link

I currently have an app that uses External JS/style files that exports different StyleSheet.create() objects, which will be reused across the app.
In your example of detecting orientation changes, you have put StyleSheet.create() inside the render, so it can be modified on every orientation change. However your example seems difficult to apply in my existing app code base, because I can't export objects from inside the render method. Do you have any solution for this?

@ahmedkamalio
Copy link
Contributor

Once you called listenOrientationChange you'll have orientation in the component's state you can use this to detect which style will be applied.

Example 1

import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { 
listenOrientationChange, 
removeOrientationListener 
} from 'react-native-responsive-screen';

class AwesomeComponent extends Component {
  componentDidMount() {
    listenOrientationChange(this);
  }
  
  componentWillUnmount() {
    removeOrientationListener();
  }

  render() {
    return (
      <View style={styles.container(this.state.orientation)}/>
    );
  }
}

const styles = StyleSheet.create({
      container: orientation => ({ 
          flex: 1,
          backgroundColor: orientation === 'portrait' ? 'blue' : 'red'
        })
    });

export default AwesomeComponent;

Example 2

import React, { Component } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { 
listenOrientationChange, 
removeOrientationListener 
} from 'react-native-responsive-screen';

class AwesomeComponent extends Component {
  componentDidMount() {
    listenOrientationChange(this);
  }
  
  componentWillUnmount() {
    removeOrientationListener();
  }

  render() {
    return (
      <View style={
                  this.state.orientation === 'portrait'
                  ? portraitStyles.container
                  : landscapeStyles.container
         }/>
    );
  }
}

// Seperated style objects

const portraitStyles = StyleSheet.create({
      container: { 
          flex: 1,
          backgroundColor: 'blue'
        }
    });

const landscapeStyles = StyleSheet.create({
      container: { 
          flex: 1,
          backgroundColor: 'red'
        }
    });

export default AwesomeComponent;

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

2 participants