I am using react-native-router-flux as a router. This is my very first RN app in which I have a Home screen, and a chat screen. My home screen is working fine, and I am able to successfully navigate to to the chat screen upon button press. However, my home screen has an enough form and the value that the use enters (name) I want to pass to the chat screen as a prop.
Main App component
import React, { Component } from 'react';
import Home from './components/Home';
import Chat from './components/Chat';
import {
Router,
Stack,
Scene,
} from 'react-native-router-flux';
class App extends Component {
render() {
return (
<Router>
<Stack key='root'>
<Scene key='home' component={Home} title='Home' />
<Scene key='chat' component={Chat} title='Chat' />
</Stack>
</Router>
)
}
}
export default App;
Home component
import React, { Component } from 'react';
import {
View,
Text,
StyleSheet,
TextInput,
TouchableOpacity,
} from 'react-native';
import {
Actions,
} from 'react-native-router-flux';
class Home extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
}
}
render() {
return (
<View>
<Text style={styles.title}>
Enter your name:
</Text>
<TextInput
style={styles.nameInput}
placeholder='why so serious??'
onChangeText={(text) => {
this.setState({name: text})
}}
value={this.state.name}
/>
<TouchableOpacity
onPress={() => {
Actions.chat({
name: this.state.name,
})
}}
>
<Text style={styles.buttonText}>
Next
</Text>
</TouchableOpacity>
</View>
)
}
}
const styles = StyleSheet.create({
title: {
marginTop: 20,
marginLeft: 20,
fontSize: 20,
},
nameInput: {
padding: 5,
height: 40,
borderWidth: 2,
borderColor: 'black',
margin: 20,
},
buttonText: {
marginLeft: 20,
fontSize: 20
}
})
export default Home;
if I alert this.state.value on the onPress() function, the value is being captured and alerted.
However,
Actions.chat({
name: this.state.name,
})
}}
isn't recieving this input value. When the app goes to the chat screen it just says "hello"
Chat component
import React, { Component } from 'react';
import {
View,
Text,
} from 'react-native';
class Chat extends Component {
render() {
return (
<View>
<Text>
Hello {this.props.name}
</Text>
</View>
)
}
}
export default Chat;
Am I simply over looking something here? any help is appreciated in passing prop from home to chat component.
edit:
In the chat component when i alert(this.props.name) it alert Hello chat. The key from <Scene key='chat' component={Chat} title='Chat' /> in the app component is being passed down instead of the input value from the home component. Not sure why this is happening
seems like name is reserved prop for action key you can do something like that
Actions.chat({
userName: this.state.name,
})
and in your chat class
class Chat extends Component {
render() {
return (
<View>
<Text>
Hello {this.props.userName}
</Text>
</View>
)
}
}
Related
I am new to react native for navigation i am using react-native-router-flux but it returns an error => development server returned response error code 500
I have uninstall and reinstall node modules
My react-native -v is 0.59.9
metro-react-native-babel-preset -v ^0.54.1
react-native-router-flux ^4.0.6
login page
import React, { Component } from 'react'
import { Text, View, StyleSheet, TextInput, TouchableOpacity } from 'react-native'
import { Actions } from 'react-native-router-flux'
export default class Login extends Component {
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Text style={styles.headerText}>Sparow</Text>
</View>
<View style={styles.content}>
<TextInput style={styles.usernameInput} placeholder='Enter Your Username' placeholderTextColor='#fff' underlineColorAndroid='transparent' />
<TextInput style={styles.passwordInput} placeholder='Enter Your Password' placeholderTextColor='#fff' underlineColorAndroid='transparent' secureTextEntry={true} />
<TouchableOpacity style={styles.loginBtn} onPress={() => { Actions.Admin() }}>
<Text style={styles.loginBtnText}>Login</Text>
</TouchableOpacity>
</View>
</View>
)
}
}
admin page
import React, { Component } from 'react'
import { View, Text } from 'react-native'
export default class Admin extends Component {
render() {
return (
<View>
<Text>Admin Page</Text>
</View>
)
}
}
routes page
import React, { Component } from 'react'
import { Router, Scene } from 'react-native-router-flux'
import Login from '../screen/Login'
import Admin from '../screen/Admin'
export default class Routes extends Component {
render() {
return (
)
}
}
routes pages
import React, { Component } from 'react';
import Routes from './src/common/Routes'
export default class App extends Component {
render() {
return (
);
}
}
I'm doing the ReactNative mobile app. WebApp runs smoothly and everything works. But when I transfer everything to the mobile part, I'm stuck with an error when I enter the text in input field. What I'm doing wrong and what is my mistake? Here is my code:
import React from 'react';
import { StyleSheet, TextInput, View } from 'react-native';
export default class App extends React.Component {
state={
message:"Hi {name}, your rating for this app is {rating}"
}
handleName=()=>{
this.setState({
message:"Hi "+this.refs.name.value+", your rating for this app is {rating}"
})
}
handleRating=()=>{
this.setState({
message:"Hi "+this.refs.name.value+", your rating for this app is "+this.refs.rating.value
})
}
render() {
return (
<View style={styles.container}>
<TextInput
placeholder="Your Name"
ref="name"
onChangeText={this.handleName}
/>
<TextInput
placeholder="Your Rating"
ref="rating"
onChangeText={this.handleRating}
/>
<View>{this.state.message}</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
I don't think that referring to the TextInput component like that (using a string reference) is the best way to achieve this. However, if you want to, you could use this.refs.name._lastNativeText to access the current value of the TextInput instead of this.refs.name.value.
In my opinion, a better way would be to use the value returned by onChangeText() callback. You could do that like this:
import React from 'react';
import { StyleSheet, TextInput, View } from 'react-native';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
name: null,
rating: null,
message: null,
}
}
render() {
return (
<View style={styles.container}>
<TextInput
placeholder="Your Name"
onChangeText={text => this.setState({name: text})}
/>
<TextInput
placeholder="Your Rating"
onChangeText={text => this.setState({rating: text})}
/>
{ this.state.name && this.state.rating &&
<Text>Hi {this.state.name}, your rating for this app is {this.state.rating}.</Text>
}
</View>
);
}
}
I haven't tested this code, comment below if you face some error.
Hello I am trying to make a Instagram like button with react-native by changing the image on press, but when I press the image no changes
Everything is up to date. And the code has no errors.
Here is my code:
import React, { Component } from 'react';
import { View, Text, StyleSheet, Image,navigation,TouchableOpacity } from 'react-native';
import { Container, Content, Badge, Left, Icon, Header} from 'native-base';
import { DrawerActions } from 'react-navigation';
class HomePage extends Component {
constructor(props){
super(props)
this.state = {
likedQ: false,
uri: require('./images/mianIcons/like.png')
}
}
_ifLiked = () => {
likedQ = true;
uri: require('./images/mianIcons/like3.png')
}
render(){
return(
<View>
<Header>
<Left>
<Icon name="ios-menu" onPress={()=> this.props.navigation.openDrawer()}
/>
</Left>
</Header>
<TouchableOpacity onPress={this._ifLiked()}>
<Image
style={{width: 32 , height: 32 ,}}
source={require(uri)}
/>
</TouchableOpacity>
</View>
)
}
}
export default HomePage;
You should set state to rerender the component
_ifLiked = () => {
this.setState({
likedQ: true,
uri: require('./images/mianIcons/like3.png')
})
}
change your onPress function like this. It is basic thing that you should set state for react components to update
There are some more corrections. Following is the corrected code
class HomePage extends Component{
constructor(props){
super(props)
this.state = {
likedQ: false,
uri: './images/mianIcons/like.png'
}
}
_ifLiked = () => {
this.setState({
likedQ: true,
uri: require('./images/mianIcons/like3.png')
}
}
render(){
return(
<View>
<Header>
<Left>
<Icon name="ios-menu" onPress={()=> this.props.navigation.openDrawer()} />
</Left>
</Header>
<TouchableOpacity onPress={() => this._ifLiked}>
<Image style={{width: 32 , height: 32}} source={require(this.state.uri)} />
</TouchableOpacity>
</View>
)}
}
I'm tying to change content of tabs in react-native-material-bottom-navigation but when I press tab it redirect me to respective page. I want to change content between header and tabs. I've tried below code but I don't know how to display content in condition statement.
HomeScreen.js
import React, {Component} from 'react';
import {Text, View } from 'react-native';
import BottomNavigation, { Tab } from 'react-native-material-bottom-navigation';
import Icon from 'react-native-vector-icons/MaterialIcons';
import HomeScreenContent from './HomeScreenContent';
import Categories from './Categories';
import Profile from './Profile';
export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.handleTabChange = this.handleTabChange.bind(this);
this.state = { activeTab: 0 };
}
handleTabChange(newTabIndex, oldTabIndex) {
this.setState({ activeTab: newTabIndex });
if (newTabIndex === oldTabIndex) {
null;
}
if (this.state.activeTab === 0) {
<HomeScreenContent />
} else if (this.state.activeTab === 1) {
<Categories/>
} else {
<Profile />
}
}
render() {
return (
<View style={styles.container}>
<BottomNavigation
activeTab={this.state.activeTab}
labelColor="#5c007a"
rippleColor="white"
style={{
height: 56,
elevation: 8,
position: 'absolute',
left: 0,
bottom: 0,
right: 0
}}
onTabChange={this.handleTabChange}
>
<Tab
barBackgroundColor="#fff"
label="Home"
icon={<Icon size={24} color="#5c007a" name="home" />}
/>
<Tab
barBackgroundColor="#fff"
label="Categories"
icon={<Icon size={24} color="#5c007a" name="list" />}
/>
<Tab
barBackgroundColor="#fff"
label="Profile"
icon={<Icon size={24} color="#5c007a" name="person" />}
/>
</BottomNavigation>
</View>
);
}
}
HomeScreenContent.js
import React from 'react';
import { TouchableOpacity, ScrollView, Image, Dimensions , StyleSheet, Text, View } from 'react-native';
export default class HomeScreenContent extends React.Component {
render() {
return (
<View>
<Text> Hello from HomeScreen</Text>
</View>
);
}
}
Categories.js
import React from 'react';
import { Text, View } from 'react-native';
export default class HomeScreenContent extends React.Component {
render() {
return (
<View>
<Text> Hello from Categories</Text>
</View>
);
}
}
Profile.js
import React from 'react';
import {Text, View } from 'react-native';
export default class HomeScreenContent extends React.Component {
render() {
return (
<View>
<Text> Hello from Profile</Text>
</View>
);
}
}
I just started react-native.
When I try to use the createMaterialBottomTabNavigator from 'react-navigatioin-tabs', I got this warning msg. I have no idea which part makes trouble from the code
maybe it's misuse something..
Could you help me out..?
code below..
import { createMaterialBottomTabNavigator
} from 'react-navigation-tabs';
import Home from '../Screens/HomeScreen/Home'
import Maps from '../Screens/HomeScreen/Maps'
import My from '../Screens/HomeScreen/My'
import Subjects from '../Screens/HomeScreen/Subjects'
const BottomTabNavigation = createMaterialBottomTabNavigator(
{
Home: { screen: Home },
Maps: { screen: Maps },
My: { screen: My },
Subjects: { screen: Subjects },
},
{
initialRouteName: 'Home',
activeTintColor: '#F44336',
}
);
export default BottomTabNavigation
BottomTabNavigation.js
import React, { Component } from 'react';
import {
NativeModules,
Text,
View,
} from 'react-native';
import styles from '../../Styles/HomeScreen/styles';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
class Home extends Component {
static navigationOptions = {
tabBarColor: '#3F51B5',
tabBarIcon: <MaterialIcons
style={{ backgroundColor: 'transparent' }}
name='photo-album'
color='red'
size={24}
/>,
};
render(){
return (
<View style={styles.container}>
<Text>Home</Text>
</View>
);
}
}
export default Home;
Home.js
The problem lies in tabBarIcon, which is defined as a function which returns another function. So you would need to invoke that inner function as well ( tabBarIcon('photo-album')() ). And another thing: where is your tintColor variable extracted from?
I would change your code, and do something like this:
const tabBarIcon = (name, tintColor) => (
<MaterialIcons
style={{ backgroundColor: 'transparent' }}
name={name}
color={tintColor}
size={24}
/>
);
class Home extends React.Component {
static navigationOptions = {
tabBarColor: '#3F51B5',
tabBarIcon: tabBarIcon('photo-album', 'SOME_COLOR'),
title: 'good job!',
};
render() { ... }
}
change import * as React from 'react'; to
import React, { Component } from 'react';
export default class Home extends Component {
}
Hope that help