ToolbarAndroid navIcon show small in 0.14 - react-native

init new project, react-native:0.14.+
index.android.js content:
<ToolbarAndroid
navIcon={require('./ic_menu_white.png')}
title='title'
titleColor="green"
style={styles.toolbar}
actions={toolbarActions}
onActionSelected={this.onActionSelected}
/>
<Image source={require('./ic_menu_white.png')} />
but this navIcon image show small size, and Image is normal,why this ?

I have exactly same issue as yours. I don't have a solution but here's the workaround:
put "ic_menu_white.png" in the /android/app/src/main/res/drawable folder.
modify ToolbarAndroid as following:
<ToolbarAndroid
navIcon={{ uri: 'ic_menu_white' }}
title='title'
titleColor="green"
style={styles.toolbar}
actions={toolbarActions}
onActionSelected={this.onActionSelected}
/>
I found this workaround here. Hope that would help.

Related

How to achieved multiple nested screens

Having trouble with my StackNavigator with two another screens in the nested, below are the nested stack I want to achieve.
Items
-Help (already achieve this nested)
-Help Details
-Topup
-Topup Details
-Services (I want to achieve something like this);
-Service name (Babysitting)
-service name details (Babysitting details)
This is ony my DashboardNavigator
function DashboardNavigator() {
return (
<StackDashboard.Navigator initialRouteName="Dashboard">
<StackDashboard.Screen name="Topup" component={TopupStack} options={navOptionHandler} />
<StackDashboard.Screen name="Services" component={ServicesStack} options={navOptionHandler} />
<StackDashboard.Screen name="BabySitting" component={BabySittingStack} options={navOptionHandler} />
</StackDashboard.Navigator>
)
}
function BabySittingStack() {
<StackBabySitting.Navigator initialRouteName="BabySitting">
<StackBabySitting.Screen name="BabySitting" component={BabySitting} options={navOptionHandler} />
<StackBabySitting.Screen name="BabySittingDetails" component={BabySittingDetails} options={navOptionHandler} />
</StackBabySitting.Navigator>
}
function ServicesStack() {
return (
<StackServices.Navigator initialRouteName="Services">
<StackServices.Screen name="Services" component={Services} options={navOptionHandler} />
<StackServices.Screen name="BabySitting" component={BabySittingStack} options={navOptionHandler} />
</StackServices.Navigator>
)
}
This will be my screen that will go to my details screen
<SafeAreaView>
<TouchableOpacity style={styles.item} onPress={() => navigation.navigate('BabySittingDetails')}>
<Text style={styles.title}>BabySitting</Text>
<Image
style={styles.imageItem}
source={IMAGE.ICON_NEXT} resizeMode="contain" />
</TouchableOpacity>
</SafeAreaView>
I'm not sure this is your problem but BabySittingDetails is accessible in BabySittingStack. BabySittingStack and ServicesStack are at the same level, so you can't access babySittingdetails like that. You can test to move your BabySittingComponent inside your ServicesStack.
But I don't think you should get this kind of error with your current navigation architecture. If it does not solve your problem then please show us your BabySittingDetails component entirely and eventually reproduce your problem on a snack expo. You can use this boilerplate to reproduce your problem (ios is working fine as well as web, android have some trouble with stylesheet i don't know why).

Displaying external SVG image from URL in react native

Is there any package that would allow me to display SVG image loaded from external URL?
I've tried to use react-native-svg-image / react-native-svg-uri and their forks but none of those are working correctly - is there any non dead package for react-native that would do the job?
This is the sample error being thrown by svg-img-uri
<SvgUri width="24" height="24" source={{ uri: 'https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.3.0/flags/1x1/ad.svg' }} />
Text strings must be rendered within a <Text> component.
This error is located at:
in RNSVGDefs (at Defs.js:8)
react-native-svg now supports display svg from online uri
import { SvgUri } from 'react-native-svg';
export default () => (
<SvgUri
width="100%"
height="100%"
uri="http://demo.com/assets/your/image.svg"
/>
);
Reference https://github.com/react-native-svg/react-native-svg#use-with-content-loaded-from-uri
The url doesnt' have any quotes around it. That might be the problem
<SvgUri
width="24"
height="24"
source={{ uri: 'https://cdnjs.cloudflare.com/ajax/libs/flag-icon-css/3.3.0/flags/1x1/ad.svg'}} />
This Github issue might help you.
If you still do not succeed, you can still convert your SVG file to a React Native Component using https://www.smooth-code.com/open-source/svgr/playground/ (don't forget to toggle React-native in the left panel)

Where to put images in a react-native project?

I am working on a react-native project and we are putting images currently in /images/ folder. Is it a good path for them ? Is there any best practice?
You can add image folder to src(src/image/index.js). Image folder add index.js file create and add whole app image.
In index.js file set
export const IMAGENAME = require('./icon.png');
When import image folder
import { IMAGENAME } from '../image';
Use image:
<Image source={ IMAGENAME } />
You can add image to image folder and set path to index file.
Hope this will help you.
To add a static image to your app, place it somewhere in your source code tree and reference it like this:
<Image source={require('./my-icon.png')} />
please see the below link for more explanation:
https://reactnative.dev/docs/images
In image assets folder, create index.js file and put following:
const images = {
main_bg: require('./background.png'),
main_logo: require('./auth/home_title.png'),
///you can add more many images like this here.
};
module.exports = images;
When using the images, you can do like this:
import Images from '../../assets/index';
...
first create file "ImagesAssets.js" in folder assets.
export const ImagesAssets = {
bannerList1: require('./silver.png'),
bannerList2: require('./gold.png'),
bannerList3: require('./dimound.png'),
bannerList4: require('./busniss.png'),
///you can add more many images like this here.
};
then import the object to "any page" and use it.
import{ ImagesAssets } from '../assets/ImagesAssets';
<View style={styles.itemsContiner}>
<View style={styles.item}>
<Image style={styles.imgBanner} source={ImagesAssets.bannerList1}/>
</View>
<View style={styles.item}>
<Image style={styles.imgBanner} source={ImagesAssets.bannerList2} />
</View>
</View>

React Native Android - How to set the logo icon in ToolBarAndroid?

I've copied a code for an AndroidToolBar from React Native's UIExplorer:
<ToolbarAndroid
logo={require('image!launcher_icon')}
navIcon={require('image!ic_menu_black_24dp')}
onIconClicked={() => this.drawer.openDrawer()}
style={styles.toolbar}
title={title}
/>
But it seems like the image!launcher_icon and image!ic_menu_black_24dp are missing. I am getting a missing error:
It seems like some a system's baked-in icon.
How can I use this icon?
How can I use my own png file?
That is old syntax. It has been changed in the recent versions.
<ToolbarAndroid
logo={require('./ic_launcher_icon.png')}
navIcon={require('./ic_menu_black.png')}
onIconClicked={() => this.drawer.openDrawer()}
style={styles.toolbar}
title={title}
/>
Check out this guide https://facebook.github.io/react-native/docs/images.html#content
https://facebook.github.io/react-native/docs/image.html#content
Try with updated syntax and define the correct path for image resource, I have made "images" folder inside "app" to store all my images at one place. You may skip them.
<ToolbarAndroid style ={{height:56, backgroundColor:'#e0e0e0'}}
logo={require('./app/images/jd_home.png')}
title={"MyApp"}
actions={[{title: 'More', icon: require('./app/images/more.png'), show: 'always'},
{title: 'Settings', icon: require('./app/images/settings.png'), show: 'always'}]}
/>

Use <Image> with a local file

The documentation says that the only way to reference a static image is to use require.
But I'm not sure where does react expect to have those images. The examples don't have any domain, so it seems like you have to go to Xcode and add them to Images.xcassets, but that didn't work for me.
Using React Native 0.41 (in March 2017), targeting iOS, I just found it as easy as:
<Image source={require('./myimage.png')} />
The image file must exist in the same folder as the .js file requiring it for "./" to work.
I didn't have to change anything in the XCode project. It just worked.
Note that the path seems to have to start with "./" or "../" and be full lower case. I'm not sure what all the restrictions are, but start simple and work forward.
Hope this helps someone, as many other answers here seem overly complex and full of (naughty) off-site links.
UPDATE: BTW - The official documentation for this is here:
https://reactnative.dev/docs/images
It works exactly as you expect it to work. There's a bug https://github.com/facebook/react-native/issues/282 that prevents it from working correctly.
If you have node_modules (with react_native) in the same folder as the xcode project, you can edit node_modules/react-native/packager/packager.js and make this change: https://github.com/facebook/react-native/pull/286/files . It'll work magically :)
If your react_native is installed somewhere else and the patch doesn't work, comment on https://github.com/facebook/react-native/issues/282 to let them know about your setup.
ES6 solution:
import DefaultImage from '../assets/image.png';
const DEFAULT_IMAGE = Image.resolveAssetSource(DefaultImage).uri;
and then:
<Image source={{uri: DEFAULT_IMAGE}} />
If loading images dynamically one can create a .js file like following and do require in it.
export const data = [
{
id: "1",
text: "blablabla1",
imageLink: require('../assets/first-image.png')
},
{
id: "2",
text: "blablabla2",
imageLink: require('../assets/second-image.png')
}
]
In your component .js file
import {data} from './js-u-created-above';
...
function UsageExample({item}) {
<View>
<Image style={...} source={item.imageLink} />
</View>
}
function ComponentName() {
const elements = data.map(item => <UsageExample key={item.id} item={item}/> );
return (...);
}
I had this exact same issue until I realized I hadn't put the image in my Image.xcassets. I was able to drag and drop it into Xcode with Image.xcassets open and after rebuilding, it fixed the problem!
To display image from local folder, you need to write down code:
<Image source={require('../assets/self.png')}/>
Here I have put my image in asset folder.
From the UIExplorer sample app:
Static assets should be required by prefixing with image! and are located in the app bundle.
So like this:
render: function() {
return (
<View style={styles.horizontal}>
<Image source={require('image!uie_thumb_normal')} style={styles.icon} />
<Image source={require('image!uie_thumb_selected')} style={styles.icon} />
<Image source={require('image!uie_comment_normal')} style={styles.icon} />
<Image source={require('image!uie_comment_highlighted')} style={styles.icon} />
</View>
);
}
I was having trouble with react-native-navigation, I created my own header component, then inserted a image - as logo - on the left before title, then when I was triggering navigate to another screen and then back again, logo was loading again, with a timeout near 1s, my file were local. My solution :
Logo.json
{"file" : "base64 big string"}
App.js
import Logo from '.../Logo.json'
...
<Image source={{uri:Logo.file}} />
We can do like below:
const item= {
image: require("../../assets/dashboard/project1.jpeg"),
location: "Chennai",
status: 1,
projectId: 1
}
<Image source={item.image} style={[{ width: 150, height: 150}]} />
This from https://github.com/facebook/react-native/issues/282 worked for me:
adekbadek commented on Nov 11, 2015
It should be mentioned that you don't have to put the images in Images.xcassets - you just put them in the project root and then just require('./myimage.png') as #anback wrote
Look at this SO answer and the pull it references
For typescript user
import { ImageSourcePropType } from 'react-native'
type Data = {
image:ImageSourcePropType
}
const data:Data = {
image:require('../.../log.png')
}
and then
<Image source={data.image}/>
You have to add to the source property an object with a property called "uri" where you can specify the path of your image as you can see in the following example:
<Image style={styles.image} source={{uri: "http://www.mysyte.com/myimage.jpg"}} />
remember then to set the width and height via the style property:
var styles = StyleSheet.create({
image:{
width: 360,
height: 40,
}
});