useWindowDimensions use in stylesheet react native - react-native

How to use useWindowDimensions in Stylesheet. It always works only inside the function. I want to get screen height and width using useWindowDimensions inside the stylesheet in react native.

useWindowDimensions is a hook, so we just can use it inside a functional component. Maybe there are other ways, but I'd like to suggest you something like that:
import React from 'react';
import { View, Text, StyleSheet, useWindowDimensions } from 'react-native';
const Main = () => {
const { styles } = useStyle();
return (
<View style={styles.container}>
<Text>Dimension Properties inside StyleSheet{'\n'}</Text>
<Text>Heigth: {styles.container.height.toString()}</Text>
<Text>Width: {styles.container.width.toString()}</Text>
</View>
)
}
const useStyle = () => {
const dimensions = useWindowDimensions();
console.log('Logging dimensions', dimensions)
const styles = StyleSheet.create({
container: {
height: dimensions.height,
width: dimensions.width,
justifyContent: 'center',
alignItems: 'center',
},
})
return { styles }
}
export { Main }
Please, let me know if this helped you.

Related

How to Add External StyleSheet for My React Native Project

How to Add External StyleSheet for My React Native Project
Add External StyleSheet
First, you must create a file to export a StyleSheet object with styles.
Style.js
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
box: {
width: '80%',
height: 150,
backgroundColor: 'red',
alignSelf: 'center',
borderRadius: 9
}
});
export { styles }
And in your component, you must import that.
import React, { Component } from "react";
import { View } from 'react-native';
import { styles } from "./Style";
class Home extends Component {
render(){
return(
<View>
<View style={styles.box}>
</View>
</View>
)
}
}
export default Home;
Finally, run the app.
If I understand you correctly, you want to add a style to your component/s.
I assume you are using a Functional Component.
A good practice is to do it by creating a style.js file, on the same folder where your component is located.
style.js
import { StyleSheet } from 'react-native';
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
backgroundColor: 'green'
}
})
export { styles }
and in your desired component you should import it.
MyComponent.js
import React from 'react'
import { View } from 'react-native'
import { styles } from './styles' //<<-- import
const MyComponent = (props) => {
. (Whatever states and handlers, etc. that your component does)
.
.
return (
<View style={styles.container}> //<<-- usage
...rest of your JSX
</View>
)
}
export default MyComponent
good luck!

How to fix "Not A Function" Error from React Native while declaring Dynamic Styles

I'm following a tutorial and I currently have this code where I am trying to change the color of a background based on a prop... This is what it looks like
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const SomeComponent = ({ bgColor }) => (
<View style={styles.wrapper(bgColor)}>
<Text style={styles.text}>3333</Text>
</View>
);
const styles = StyleSheet.create({
wrapper: color => ({
flex: 1,
backgroundColor: color,
}),
text: {
color: 'red',
},
});
I keep getting a styles.wrapper is not a function
How do I fix this?
You cannot declare a StyleSheet function like you did.
Alternatively, you can declare a JS function that passes the same style with different colors according to the arguments you pass.
You can do that as follows:
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
const myStyle = (color) => {
return {
flex: 1,
backgroundColor: color,
}
};
const SomeComponent = ({ bgColor }) => {
<View style={this.myStyle(bgColor)}>
<Text style={styles.text}>3333</Text>
</View>
};
const styles = StyleSheet.create({
text: {
color: 'red',
},
});

React native StyleSheet.create where should I put

I need to pass props into Styles. So I created StyleSheet inside the class. But normal practice would be to create StyleSheet outside from the class.
I want to know are there any performance drawbacks by having StyleSheet.create inside the class ?
import React from 'react'
import { StyleSheet, View } from 'react-native'
import { connect } from 'react-redux'
import { Text } from 'native-base'
import p from '../../assets/colors/pallets'
const EmptyContainer = (props) => {
const texts = props.texts
const styles = StyleSheet.create({
emptyContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 20,
backgroundColor: p.background2[props.theme],
},
text: {
color: p.text1[props.theme],
},
})
return (
<View style={styles.emptyContainer}>
{texts.map((text) => (
<Text style={styles.text} key={Math.random()}>
{text}
</Text>
))}
</View>
)
}
const mapStateToProps = (state) => {
const { theme } = state
return {
theme: theme.theme,
}
}
export default connect(mapStateToProps)(EmptyContainer)
Edited:
There are several ways to pass props into StyleSheet.
Having StyleSheet inside class itself.
Pass props as function parameter to styles i.e. styles(props.theme). emptyContainer
What would be the best way by considering the performance of the app ?

react native expo read data from object

enter image description here
My code
import { StatusBar } from 'expo-status-bar';
import React, { useState } from 'react';
import { StyleSheet, Text, View, Button } from 'react-native';
import Constants from 'expo-constants';
import * as MediaLibrary from 'expo-media-library';
import * as Permissions from 'expo-permissions';
export default function App() {
async function permissions(){
console.log("printing cameraroll")
const {sp}= await Permissions.askAsync(Permissions.CAMERA_ROLL)
const s=await MediaLibrary.getAlbumsAsync("title")
console.log(s);
}
return (
<View style={styles.container}>
<Button title="Click-me" onPress={permissions} />
<StatusBar style="auto" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding:Constants.statusBarHeight,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
padding:Constants.statusBarHeight
},
});
i am trying to create a gallery type which will be part of my app so i want to read the title but i am unable to access the object
If you want to get a list of all titles you can do it like this:
const listOfTitles = MediaLibrary.getAlbumsAsync().map(album => album.title);
In case "getAlbumsAsync" returns a promise (documentation doesn't say so), then you can get it like this:
const albums = await MediaLibrary.getAlbumsAsync();
const listOfTitles = albums.map(album => album.title);

In React Native, how to import another stylesheet into current one?

I'm a novice.
Now i have to managing two or more stylesheet files in my project.
My current stylesheet code:
'use strict';
import { Platform } from 'react-native'
var React = require('react-native');
var {StyleSheet,} = React;
import {NavBar, TabBar} from './UiConfig'
import MyComponents from './styles/MyComponents'
const MyStyleTheme = StyleSheet.create({
middle: {
alignItems: "center",
justifyContents: "center"
},
NavBar: {
backgroundColor: NavBar.bgColor //from UiConfig
}
})
export default MyStyleTheme
And the another file:
'use strict';
import { Platform } from 'react-native'
var React = require('react-native');
var {StyleSheet,} = React;
const MyComponents = StyleSheet.create({
Navigator: {
...
...
}
})
export default MyComponents
So...it's possible to import a primary stylesheet instead of import another one just like CSS #import or not?
index.ios.js:
import React, { Component } from 'react'
import {
StyleSheet,
StatusBar,
Text,
View,
} from 'react-native'
import MyStyleTheme from '../primaryStyles'
...
...
...
render(){
return(
<View style={[MyStyleTheme.middle]}>
<View style={[MyStyleTheme.MyComponents]}>
</View>
);
}
Well...I found a way to solve it:
const obj = { flex: 1, justifyContent: "center", alignItems: "center" }
const MyComponents = StyleSheet.create({
Navigator: {
...obj,
height: 44
}
})