react-native-router-flux. How to change custom navBar programmatically? - react-native

I have a custom navBar in my scene:
<Scene key="myPage"
component={MyPage}
navBar={NavBarCustom}
hideNavBar={false}/>
....
class NavBarCustom extends Component {
constructor(props) {
super(props);
}
onExitPressed(){
App.exit();
}
render() {
return (
<View style={styles.navBar}>
<View style={styles.leftContainer}>
<Image
style={styles.logo}
source={require('./../../res/ic_nav_bar.png')}/>
<Text style={[appStyles.customFontBold, styles.title1]}>
MY TITLE
</Text>
</View>
<View style={styles.centralContainer}>
<Text style={[appStyles.customFontRegular, styles.title2]}>
{strings.benefit_identifier}
</Text>
</View>
<View style={styles.rightButtonContainer}>
<TouchableHighlight
style={{padding: 7}}
underlayColor='#b59d6e'
onPress={() => { this.onExitPressed() }}>
<Text style={[appStyles.customFontRegular, styles.rightButtonTitle]}>
{strings.exit}
</Text>
</TouchableHighlight>
</View>
</View>
);
}
}
It works good. So how can I change title1 of NavBarCustom from my scene MyPage?
Thanks in advance.

You can pass/send information through/with props:
In your render you can declare a const which take the react-native-router-flux Actions, set the route which to point and then set the object which to pass:
If there's a Login main file, which then redirects to a Register view then you declare the const goToRegister and then pass the text with its content:
class Login extends Component {
render() {
const goToRegister = () => Actions.Register({text: 'From Login'});
return(
<View style={{flex:1}}>
<Text>
Login
</Text>
<TouchableHighlight onPress={goToRegister} style={{ marginTop: 100}}>
<Text>Go Register</Text>
</TouchableHighlight>
</View>
)
}
}
Then within your Register you receive it just within your props as this.props.text:
class Register extends Component {
render() {
return(
<View style={{flex:1}}>
<TouchableHighlight onPress={Actions.Login} style={{ marginTop: 100}}>
<Text>{ this.props.text }</Text>
</TouchableHighlight>
</View>
)
}
}
In your case you should send firstly the value of your title maybe as:
render() {
const goToMyPage = () => Actions.MyPage({ title: 'Some Title'})
...
And then you're able to use it:
<Text style={[appStyles.customFontBold, styles.title1]}>
{ this.props.title }
</Text>

Scene takes a title param
<Scene
key="myPage"
component={MyPage}
navBar={NavBarCustom}
hideNavBar={false}
title="myPage"
/>

Related

How to save state of a page after getting data from a route

Image, title, price and description are saved in the database (SQlite
i send the saved data to the listing screen with navigation
const sendMeToNewList=()=>{
return(
<View>
{
dataFromDatabase &&
navigation.navigate('listings',{image:image, title:title, price:price, description:description})
}
</View>
)
}
function ListingScreen({navigation}, props) {
const route = useRoute();
const myroute = route.params
return (
<View style={{flex: 1}}>
<ScrollView>
<View style={styles.container}>
<View style={styles.cardContainer}>
{
!myroute &&
<View>
<TouchableOpacity activeOpacity="0.6" onPress ={()=> navigation.navigate("listingdetail")}>
<Image style={styles.image}
source={require('../assets/redphone.jpg')}
/>
</TouchableOpacity>
<Text style={styles.productTitle}>RedIphone for Sale</Text>
<Text style={styles.productPrice}>$950</Text>
</View>
}
{
myroute &&
<View>
<TouchableOpacity activeOpacity="0.6" onPress ={()=> navigation.navigate("listingdetail")}>
<Image style={styles.image}source={{ uri: myroute.image }} />
</TouchableOpacity>
<Text style={styles.productTitle}>{myroute.title}</Text>
<Text style={styles.productPrice}>{myroute.price}</Text>
<Text style={styles.productDescription}>{myroute.description}</Text>
</View>
}
)
The above snippet will display the data with the image but once I refresh the page it vanishes. How can I save the data and pass it through to another screen?

How to disable TouchableOpacity when pressed? React-Native

How can I disable TouchableOpacity when I click on it?
This is my code:
<TouchableOpacity onPress={() => this._pickupHandler(item.p, deliv.party, index)}>
<View style={styles.carg}>
<Text style={styles.delivered_text}>Ok{deliv.loca} {deliv.addres} {deliv.pos} </Text>
</View>
</TouchableOpacity>
just pass disabled prop to TouchableOpacity for your example
export default class Touchable extends Component {
constructor(props) {
super(props);
this.state = {
disabled: false,
};
}
onPressButton = (state) => {
this.setState({
disabled: state,
});
};
render() {
return (
<TouchableOpacity disabled={this.state.disabled} onPress={() => {
this.onPressButton(true);
this._pickupHandler(item.p, deliv.party, index)
}}>
<View style={styles.carg}>
<Text style={styles.delivered_text}>Ok{deliv.loca} {deliv.addres} {deliv.pos} </Text>
</View>
</TouchableOpacity>
);
}
}
Okay so you need another state which will refer to if you want to disable it.
lets make a state var TODISABLED and set it to false initially .
Now suppose you want to disable it after 1 click , you can do this by setting state to true after click :
<TouchableOpacity disabled={this.state.TODISABLED} onPress={() => {this._pickupHandler(item.p, deliv.party, index);this.setState({TODISABLED:true})}}>
<View style={styles.carg}>
<Text style={styles.delivered_text}>Ok{deliv.loca} {deliv.addres} {deliv.pos} </Text>
</View>
</TouchableOpacity>
Hope it helps. feel free for doubts

Change TextInput editable attribute when I press a button (Not working)

I am trying to change editable with state with the click of a button, but it is not working for some reason. I have seen other people do it this way and it works. Is there another way to do this instead? or am i missing something? Thanks
`class Settings extends Component {
constructor(props) {
super(props);
this.state = {
editable: false,
name: '',
};
this.handleEdit = this.handleEdit.bind(this);
this.handleName = this.handleName.bind(this);
}
handleEdit() {
this.setState({
editable: !this.state.editable,
});
}
handleName = (text) => {
this.setState({
name: text,
});
};
render() {
return(
<View style={styles.container}>
<View style={styles.headerContainer}>
<Text style={styles.header}>Settings</Text>
</View>
<View style={styles.section}>
<View style={styles.sectionTitleContainer}>
<Text style={styles.sectionTitle}>My Account</Text>
</View>
<View>
<Text>Name:</Text>
<TextInput
placeholder="name"
value={this.state.name}
onChangeText={this.handleName}
editable={this.state.editable}
/>
</View>
<View>
<TouchableOpacity onPress={() => this.handleEdit}>
<Text>Edit</Text>
</TouchableOpacity>
</View>
</View>
</View>
);
}
}
export default Settings;`
Change your
<TouchableOpacity onPress={() => this.handleEdit}>
To
<TouchableOpacity onPress={this.handleEdit}>
I believe that since you already binded 'this' to the handleEdit function you dont need to pass the () => anymore.

state disappears when method is call

I'm working on a class project and my state is disappearing. After componentDidMount console.log(this.state) is fine. I initiate setInterval and call inc(). Somehow when I enter inc() the state gets wiped out.
import React from 'react';
import { TextInput,Button,StyleSheet, Text, View } from 'react-native';
import styles from './styles/styles.js';
debug=true
export default class App extends React.Component {
constructor(){
super()
this.state={timer:'WORK',
workTime: 25*60+0,
breakTime: 5*60+0,
currentTime:0,
remainingTime:null,
min:0,
sec:0,
startFlag:false,
resetFlag:false}
}
componentDidMount(){
this.interval=setInterval(this.inc,10000)
if(debug)console.log('COMPONENTDIDMOUNT',this.state)
}
static getDerivedStateFromProps(nextProps, prevState) {
if(debug)console.log('GETDERIVEDSTATEFROMPROPS',prevState)
return null
}
shouldComponentUpdate(nextProps,nextState){
if(debug)console.log('SHOULDCOMPONENTUPDATE',nextState)
return true
}
componentDidUpdate(){
if(debug)console.log('COMPONENTDIDUPDATE',this.state)
}
componentWillUnmount(){
if(debug)console.log('COMMPONENTWILLUNMOUNT',this.state)
}
startToggle(){
if(endTime === null)this.setState({remainingTime:this.state.workTime,
startFlag:!this.state.startToggle})
else this.setState({remainingTime:!this.state.startFlag})
}
textTime(){
let min = Math.floor(this.state.remainingTime / 60).toString()
let sec = (this.state.remainingTime % 60)
if (sec < 10)sec ? '0' + sec : sec.toString()
this.setState({min:min,sec:sec})
}
inc(){
console.log(this.state)
}
captureInput(){}
render() {
console.log('RENDER',this.state)
return (
<View style={styles.container}>
<Text style={styles.bigFont}>{`${this.state.timer + 'TIMER'}`}</Text>
<Text style={styles.bigFont}>12:00</Text>
<View style={styles.button}>
<Button title='START' onPress={()=>this.startToggle()} />
<Button title='RESET' onPress={()=>this.resetToggle()} />
</View>
<View style={styles.row}>
<Text style={[styles.bold,{marginRight:10},{width:112},
{textAlign:'right'}]}>
'Work Timer:'</Text>
<Text style={styles.bold}> min:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
<Text style={styles.bold}> sec:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
</View>
<View style={styles.row}>
<Text style={[styles.bold,{marginRight:10},{width:112},
{textAlign:'right'}]}>
'Break Timer:'</Text>
<Text style={styles.bold}> min:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
<Text style={styles.bold}> sec:</Text>
<TextInput
defaultValue='50'
style={styles.input}
onChangeText={(text) => {this.captureInput(text)}}
/>
</View>
</View>
)
}
}
You have 2 options:
Change inc() to inc = () =>
or
Change this.inc to this.inc.bind(this)
Change your inc method declaration to
inc = () => {
...
}
As per your code, this inside inc() is not referring to the component, hence you are not getting state either.
Hope this will help!

how to move the view up when keyboard is showing using react-native

native-keyboardevents for move the view up when keyboard is showing, i wrote my code like this
var KeyboardEvents = require('react-native-keyboardevents');
var KeyboardEventEmitter = KeyboardEvents.Emitter;
class Account extends React.Component{
constructor(props, context){
super(props);
this.state = {
keyboardSpace: 0,
isKeyboardOpened: false
};
}
updateKeyboardSpace(frames){
this.setState({
keyboardSpace: frames.end.height,
isKeyboardOpened: true
});
}
resetKeyboardSpace(){
this.setState({
keyboardSpace: 0,
isKeyboardOpened: false
});
}
componentDidMout(){
KeyboardEventEmitter.on(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
KeyboardEventEmitter.on(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
}
componentWillunmount(){
KeyboardEventEmitter.off(KeyboardEvents.KeyboardDidShowEvent, this.updateKeyboardSpace);
KeyboardEventEmitter.off(KeyboardEvents.KeyboardWillHideEvent, this.resetKeyboardSpace);
}
render() {
var paddingTop = 110 - this.state.keyboardSpace /3;
return (
<View style={styles.container}>
<View style={[styles.header,{paddingTop: paddingTop}]}>
<Image style={styles.image} source={require('image!pic1')} />
</View>
<View style={styles.main}>
<Text style={styles.joinNow}>Join Now</Text>
<TouchableHighlight style={styles.sub} >
<Text style={styles.signin}>Sign In</Text>
</TouchableHighlight>
</View>
<View style={styles.body}>
<View style={styles.inputContainer}>
<Image style={styles.inputUsername} source={require('image!mail')}/>
<TextInput
style={styles.email}
value={this.state.email}
placeholder="Email"/>
</View>
<View style={styles.inputContainer}>
<Image style={styles.inputPassword} source={require('image!locker')}/>
<TextInput
password={true}
style={styles.email}
value={this.state.password}
placeholder="Password"/>
</View>
<View>
<TouchableHighlight style={styles.button}>
<Text style={styles.buttonText}>JOIN</Text>
</TouchableHighlight>
</View>
</View>
</View>
)
}
}
when i tried like this the padding show top of the view, but i want that the view to move upward when the keyboard as shown,can any one give me suggestions how to solve this,any help much appreciated