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
}
})
Related
I'm building a React Native application in expo, and am trying to use a gif file for a splash animation. I've configured my code as follows using expo-splash-screen, however nothing is rendered when I try to load the app. Any suggestions?
Here's the root of my app App.js
import 'react-native-gesture-handler';
import React, { useCallback,useState } from 'react'
import { StyleSheet, Text, View } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { MyStack } from './routes/homeStack';
import { useFonts } from 'expo-font';
import * as SplashScreen from 'expo-splash-screen'
import * as Sentry from 'sentry-expo';
import Splash from './components/Splash';
SplashScreen.preventAutoHideAsync();
export default function App() {
const [appIsReady, setAppIsReady] = useState(false);
const [ fontsLoaded ] = useFonts({
'Sofia-Pro': require('./assets/Sofia_Pro_Regular.otf'),
'Helvetica-Neue': require('./assets/HelveticaNeue-Medium.otf')
})
const onLayoutRootView = useCallback(async () => {
if (fontsLoaded) {
await SplashScreen.hideAsync();
}
}, [fontsLoaded]);
if (!fontsLoaded) {
return <Splash/>
}
return (
<NavigationContainer onLayout={onLayoutRootView}>
<MyStack/>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
And here's my splash animation component Splash.tsx
import React from 'react'
import { Image,StyleSheet,View } from 'react-native'
const Splash = () => {
return (
<View style = {styles.container}>
<Image
style = {styles.image}
source ={require('../assets/splash_animation.gif')}
/>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
image: {
width: '100%',
height: '100%',
resizeMode: 'contain',
},
});
export default Splash
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.
I have this in my Comp:
import { StatusBar } from 'expo-status-bar';
import React, { Component, } from 'react';
import { StyleSheet, Text, TextInput, View, WebView } from 'react-native';
function AccurXVCS() {
return(
<View>
<WebView source={{html:"<html><body style='color:red'>Hello<br/>This is a test</body></html>"}} />
</View>
);
}
export default AccurXVCS;
And this in my App Comp:
import { StatusBar } from 'expo-status-bar';
import React, {useState, useEffect } from 'react';
import { StyleSheet, Text, View } from 'react-native';
import AccurX from './VideoCallingService/AccurXVCS';
export default function App() {
const [count, setCount] = useState(0);
useEffect(() => {
});
return (
<View style={styles.container}>
<AccurX></AccurX>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
why am i getting this error?
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);
I am having a problem solving this error. Do I need to render using class components only? or is there a way to use the function method?
Help me if you can.
What would be my best solution?
I have attached code below
EstesGuideNavigator.js
import {createStackNavigator} from 'react-navigation-stack';
import {createAppContainer} from 'react-navigation';
import CategoriesScreen from '../screens/CategoriesScreen';
import PlaceScreen from '../screens/PlacesScreen';
import PlacesDetailScreen from '../screens/PlacesDetailScreen';
const EstesGuideNavigator= createStackNavigator({
Categories: CategoriesScreen, //mapping CategoriesScreen to Categories which makes easier to navigate
Places: {
screen: PlaceScreen
},
PlacesDetail:PlacesDetailScreen
});
export default createAppContainer(EstesGuideNavigator);
Below would be App.js
import React, {useState}from 'react';
import { StyleSheet, Text, View } from 'react-native';
import *as Font from 'expo-font';
import {Apploading} from 'expo';
import EstesGuideNavigator from './navigation/EstesGuideNavigator';
const fetchFonts = () => { //fetching costum fonts for my app using Async
Font.loadAsync({
'raleway-blackItalic' : require('./assets/fonts/Raleway-BlackItalic.ttf'),
'raleway-bold' : require('./assets/fonts/Raleway-Bold.ttf'),
'raleway-regular' : require('./assets/fonts/Raleway-Regular.ttf'),
'raleway-thin' : require('./assets/fonts/Raleway-Thin.ttf')
});
};
export default function App() {
const [fontLoaded, setFontLoaded] = useState(false); //initially it's false because app hasn't been loaded
if (!fontLoaded) {
return(
<Apploading
startAsync = {fetchFonts}
onFinish = {() => setFontLoaded(true) }
/> //if assets(fonts here) is not loaded we display loading screen and load assets for app
);
}
return <EstesGuideNavigator/>;
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
}
});
CategoriesScreen.js
import React from'react-native';
import {View, Text, StyleSheet} from 'react-native';
const CategoriesScreen = props =>{
return(
<View style ={styles.screen}>
<Text> Categories Screen! Dummy </Text>
</View>
);
};
const styles = StyleSheet.create({
screen:{
flex:1,
justifyContent:'center',
alignItems:'center'
}
});
export default CategoriesScreen;
This is just a dummy screen that i wanted to create.
It was a wrong import that react native automatically changed in my CategoriesScreen.js.