How can I change the color of the component that I created after calling? - react-native

For the below logout component I want to change its TintColor and text color , what should I code?
I tried adding style here but it doesn't work.
<MenuItems listingname="Logout"
onPress={() => {
this.RBSheet.close();
this.props.notifyCountrUpdtHndlr(0)
AuthHelpers.logout(this.props.navigation,true);
}}
imagesource ={IMAGE.LOGOUT_ICO}/>

you can go inside MenuItems component and change the style there
(click on MenuItems component name and press F12 key)
just find Image and Text Component and add your styles
(to find Image and Text component inside MenuItems search listingname and imagesource)
<Image style={yourNewStyle}/>
<Text style={yourNewStyle}> </Text>

The very basic method you can use Inline CSS
<div style={{ backgroundColor: 'red' }}>
This component has a red background
</div>
And Another Method is Using CSS class selector
.red-background {
background-color: red;
}
// component
<div className="red-background">
This component has a red background
</div>
Also you can change the color of the component dynamically like
import React, { Component } from 'react';
import { View, Text } from 'react-native';
class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
backgroundColor: '#F5FCFF'
};
}
changeColor = () => {
this.setState({
backgroundColor: '#123456'
});
}
render() {
return (
<View style={{backgroundColor: this.state.backgroundColor, height:100, width: 100}}>
<Text onPress={this.changeColor}>Click Me!</Text>
</View>
);
}
}
export default MyComponent;

Related

Styling custom component in react native

I am trying to add styling to my custom component in react native, but no matter what I do, the style has no effect. Here is my code:
// App.js
import MyCustomComponent from './components/myCustomComponent.js';
render() {
return (
<View style={styles.container}>
<MyCustomComponent style={{marginTop: 10}}/>
</View>
);
}
The project compiles fine, and my custom component appears on screen fine, but the marginTop styling is not applied. It is worth noting that the style for the parent View component does apply correctly. This is a brand new project I just created today. This seems like it should be extremely basic, but just isn't working. What can I do to apply this styling?
Custom component code:
import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';
type Props = {};
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Image
source={{ uri: "source here" }}
style={{ width: 50, height: 50 }}
/>
<TextInput
style={{ height: 50 }}
placeholder="Search"
/>
</View>
)
}
}
you can use this code:
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={[styles.container, {...this.props.style}]}>
...
</View>
)
}
}
now, styles.container is applied and anything you pass to component through style will be added to component style.
I hope this can help you
You can apply a style to your custom component by passing style as props.
and
Use it as style={this.props.style} in your MyCustomComponent.
import React, {Component} from 'react';
import {TextInput, StyleSheet, Image, View, Button} from 'react-native';
type Props = {};
export default class MyCustomComponent extends Component<Props> {
render() {
return (
<View style={[styles.container,{...this.props.style}]}>//<--Use like this---
<Image
source={{ uri: "source here" }}
style={{ width: 50, height: 50 }}
/>
<TextInput
style={{ height: 50 }}
placeholder="Search"
/>
</View>
)
}
}
add this code in your CustomText.js file (custom component):
import React from 'react'
import {Text, StyleSheet} from 'react-native'
const CustomText = props => {
return (<Text {...props} style={{...styles.text, ...props.style}}>{props.children}</Text>);
}
export default CustomText;
const styles = StyleSheet.create({
text:{
color: '#000'
}
})
and use in the file:
<CustomText style={styles.text}>My text</CustomText>
const styles = StyleSheet.create({
text:{
fontSize: 20,
}
});
this code merge styles and pass all property to the custom components.
For example, lets change background color of custom card.
Custom Card:
export default function MyCard({color}) {
return (
<View style={[styles.card, {backgroundColor: color}]}>
</View>
)
}
In another file
<MyCard color={"pink"} />
Here, styles.card is the style added in Custom Card file and the color is given during component use.
Note: MyCard({color}) if you miss to add highlight parentheses, it will not work. I faced this issue.
You need to apply this style yourself inside MyCystomComponent. For example:
const MyCustomComponent = ({style}) => (
<View style={style}> // This will be the style that is passed as a prop.
</View>
);

Update an input field in the webview from react-native component

I have a webview component like this:
export default class Home extends React.Component {
onMessage(m) {
//Update an input field in the webview with a custom value
}
render(){
let jsCode = '';
return (
<WebView
ref={(webView) => { this.webView.ref = webView; }}
injectedJavaScript={jsCode}
url={this.state.url}
onMessage={m => this.onMessage(m)}
/>
)
}
}
The webpage has an input field with an id='inpOne'. onMessage prop is triggered by a button click inside the webpage. How to update the input field with the above id when the onMessage prop is executed?
Stripped most of the code for brevity.
Probably like this.
export default class Home extends React.Component {
onMessage(m) {
const js = `document.getElementById('inpOne').value = ${m};`
this.webView.injectJavaScript(js);
}
}
Also, check your WebView's ref prop definition. It looks incorrect. Should be ref={ref => (this.webView = ref)}
Here is the full code of how to change HTML inside of WebWiew from React Native
import React, { Component } from 'react';
import { Text, View, TouchableHighlight } from 'react-native';
import { WebView } from 'react-native-webview';
export default class Sample extends Component {
constructor(props) {
super(props);
}
sendDataFromReactNativeToWebView() {
let injectedData = `document.getElementById("login_field").value = 'xyz#github.com';`;
this.webView.injectJavaScript(injectedData);
}
render() {
return (
<View style={{ flex: 1, marginTop: 30 }}>
<TouchableHighlight style={{ padding: 10, backgroundColor: 'gray', marginTop: 20 }} onPress={() => this.sendDataFromReactNativeToWebView()}>
<Text style={{ color: 'white' }}>Send Data To WebView from React Native</Text>
</TouchableHighlight>
<WebView
style={{ flex: 1 }}
source={{ uri: 'https://github.com/login' }}
ref={(webView) => this.webView = webView}
/>
</View>
);
}
}

react native make part of TextInput to be bold or italics at run time

I have a simple TextInput App like below:
export default class App extends Component<Props> {
constructor(props) {
super(props);
this.state = { textValue: '' };
this.handleTextInputChange = this.handleTextInputChange.bind(this);
}
handleTextInputChange(input) {
this.setState({textValue: input})
}
render() {
return (
<KeyboardAvoidingView
behavior="padding"
style={{flex:1}}
enabled>
<TextInput
style={styles.textInputStyle}
multiline={true}
onChangeText={this.handleTextInputChange}
value={this.state.textValue}
/>
</KeyboardAvoidingView>
);
}
}
What I'd like to do is when I write ##hello in TextInput, what's instantaneously rendered in TextInput screen is hello in bold just like Markdown editing in Dropbox Paper. Similarly, when I write _hello_, what I see in the screen is hello italicized.
Screen
Can I do that? (Have part of TextInput to have different styles)
So far, it seems like TextInput can only take one style?
If we cannot have different styles TextInput, what might be an alternative to make part of (some kind of TextInput) bold, italicized, bigger, smaller...
I'm pretty sure you can nest Text within TextInput like this:
<TextInput>
<Text style={{fontWeight:'bold'}}>I'm bold</Text>
</TextInput>
Just parse the text and append Text with different styles as needed.
You can use this lib react-native-easy-markdown to render markdown text and hide the text input like this and render the markdown component instead. :
import React, { Component } from 'react';
import { StyleSheet, View, TextInput, TouchableOpacity } from 'react-native';
import Markdown from 'react-native-easy-markdown';
export default class App extends Component {
state = { text: 'type here ...' };
onClick = e => {
this.textInput.focus();
};
render() {
return (
<View style={styles.container}>
<TextInput
ref={ref => (this.textInput = ref)}
style={{ position: 'absolute', left: -1000, top: -1000 }}
onChangeText={text => this.setState({ text })}
/>
<TouchableOpacity onPress={this.onClick}>
<Markdown>{this.state.text}</Markdown>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
});
here is a demo of the code :

React Native bottom modal with horizontal scroll

I'm trying to get a modal working to look like the picture below. I've tried various modal and actionsheet solutions but can't quite get it right. Does anyone know if a solution exists that can provide a similar result? Thanks]1
I have been using an action sheet from a library (pic below) but it cannot be customized to scroll horizontally and use custom buttons. I also have not yet attempted in creating my own, I first wanted to know if anyone knows of a component which will yield the same result.
Regular action sheet on iOS
Here is a simple working example as per your requirement. I am using react-native-modal for Modal component.
import React, { Component } from 'react'
import {
StyleSheet,
Text,
View,
TouchableOpacity,
ScrollView
} from 'react-native'
import Modal from 'react-native-modal'
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
visible: false
}
}
showModal = () => this.setState({visible: true})
hideModal = () => this.setState({visible: false})
render() {
return (
<View style={{flex: 1}}>
<TouchableOpacity
onPress={this.showModal}
style={{alignSelf: 'center', marginTop: 50, backgroundColor: 'grey'}}
>
<Text>Touch Me</Text>
</TouchableOpacity>
<Modal
style={styles.modal}
isVisible={this.state.visible}
onBackdropPress={this.hideModal}
>
<ScrollView
horizontal={true}
>
{/* place your buttons here */}
<Text> Very Very Long String </Text>
</ScrollView>
</Modal>
</View>
)
}
}
const styles = StyleSheet.create({
modal: {
margin: 0,
backgroundColor: 'white',
height: 100,
flex:0 ,
bottom: 0,
position: 'absolute',
width: '100%'
}
})

why does react native display a scene above a navigator navbar component?

Consider this screenshot,
The white area on top represents a generic <View> component while the green/blue combination represents an app specific <NavBar> component mounted to the <Navigation> obj by setting the prop navigationBar=
main code from index.ios.js
var {
AppRegistry,
StyleSheet,
Text,
View,
Navigator,
} = React;
var styles = StyleSheet.create({
mainContent:{
flex:1
},
});
class BioStream extends React.Component{
render() {
return (
<Navigator navigationBar={<NavBar />} renderScene={ (route, nav) => <View style={styles.mainContent}><Text>'some text'</Text></View> } />
);
}
};
AppRegistry.registerComponent('BioStream', () => BioStream);
and main code from the NavBar component definition file,
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var styles = StyleSheet.create({
mainContainer:{
flex:1
},
toolbar:{
backgroundColor:'#81c04d',
paddingTop:20,
paddingBottom:10,
flexDirection:'row',
borderColor: 'black',
borderWidth: 1
},
toolbarButton:{
width: 50,
color:'#fff',
textAlign:'center'
},
toolbarTitle:{
color:'#fff',
textAlign:'center',
fontWeight:'bold',
flex:1
},
content:{
backgroundColor:'blue',
flex:1 //Step 2
}
});
class NavBar extends React.Component{
render() {
return (
<View style={styles.mainContainer} >
<View style={styles.toolbar}>
<Text style={styles.toolbarButton}>Add</Text>
<Text style={styles.toolbarTitle}>This is the title</Text>
<Text style={styles.toolbarButton}>Like</Text>
</View>
<View style={styles.content}>
</View>
</View>
)
}
};
module.exports = NavBar;
What am I failing to understand about how this layout (and/or the general nature of react native layouts) should be constructed? It is my intent to place the navbar consistently in all scenes at the top of the frame and have all scene content/code get rendered below.
It's seems because react native component render navigation bar after scene (source code here).You can set styles to your custom navigation bar to get it act well.
There's no issue related with this,you can open one on react native project to get the reason why it should be like this.
It is now handled by react native i guess (focus on style={styles.navigator}):
<Navigator
style={styles.navigator}
navigationBar={
<NavBar />
}
configureScene={this._configureScene}
initialRoute={this.state}
ref="featuresNavigator"
renderScene= {this._renderScene}
/>
And the style :
var styles = StyleSheet.create({
navigator: {
flexDirection: 'column-reverse'
},
});
More :
if the navbar don't appear, you have to set an height in its style <NavBar style={{height: 40}}/>