how to get value from react-native-numeric-input - react-native

I am new to react native. I am using this package https://www.npmjs.com/package/react-native-numeric-input .
My problem is how to get the value pass to the next page.
From using this code:
class FirstActivity extends Component {
static navigationOptions = {
title: 'Main Screen'
}
constructor(props) {
super(props)
this.state = {number: ''}
}
Send_Data_Function = () => { this.props.navigation.navigate('Second', { no: this.state.number, }); }
render() {
return (
<NumericInput value={this.state.value} onChange={value => this.setState({number : value})}
<TouchableOpacity activeOpacity={.4} style={styles.TouchableOpacityStyle} onPress={this.Send_Data_Function} >
<Text style={styles.TextStyle}> NEXT </Text>
</TouchableOpacity>/>
)}
class SecondActivity extends Component{
static navigationOptions =
{
title: "Second Activity"
};
render() {
return (
<View style= {styles.MainContainer}>
<Text style={styles.textStyle}>
1 = {this.props.navigation.state.params.no}
</Text>
</View>
)}
}
const AppNavigator = createStackNavigator({
First : {screen: FirstActivity},
Second : {screen: SecondActivity}
});
export default createAppContainer(AppNavigator);
How to get the result? Anyone can help me?

Your increased and decreased value pass successfully on the next page but you are not passing right state, You has to pass number state in the second activity.
Send_Data_Function = () => {
this.props.navigation.navigate("Second", { no: this.state.number });
};
and read successfully by 1 = {this.props.navigation.state.params.no} code.
Your main problem is when you press to increase or decrease button, value in the middle of these buttons won't change right?
Here is your solution.
when you initialize your number state don't assign string value.Instead of string initialize number state with number
Your code:
constructor(props) {
super(props)
this.state = {number: ''}
}
Changes:
constructor(props) {
super(props)
this.state = {number: 0}//<-------------
}
Next change is you likely forgot to assign an initial value to NumericInput component that's why it won't show you any changes in state. Also same here you used the wrong state to assign value and show value. You have an initialized number state and use value state to assign the value.
Here is your solution.
Your code:
<NumericInput
value={this.state.value}//<----mistake---
onChange={value => this.setState({number : value})}/>
Solution:
<NumericInput
initValue={this.state.number}//<---Add---
value={this.state.number}//<----changes---
onChange={value => this.setState({number: value })}/>

First you need to set the state for the changing value of numeric input.
please try to do as following:
...
constructor(props) {
super(props);
this.state = {
numericInputValue: ''
}
}
onChangeNumericInputValue = (value) => {
this.setState({ numericInputValue: value });
}
render() {
...
<NumericInput type='up-down' onChange={this.onChangeNumericInputValue} />
...
}
...
The next step is to pass the numericInputValue to the next page.
if you're using react-navigation for navigating between scenes, you can do it as following:
this.props.navigation.navigate('SceneName', { numericInputValue: this.state.numericInputValue })

Try this in your secondActivity
-->
class SecondActivity extends Component{
static navigationOptions =
{
title: "Second Activity"
};
constructor(props){
super(props);
this.state={
numericInputValue:props.navigation.getParam("no",null)
}
}
render() {
return (
<View style= {styles.MainContainer}>
<Text style={styles.textStyle}>
1 = {this.state.numericInputValue}
</Text>
</View>
)}
}

Related

React Native: How to show screen state variable updates in HeaderRight custom component using Context Api

I'm new to React Native and I want to pass state variable cartTotal from Cart component to the custom HeaderRight CartCounter component so that when I update cartTotal variable in Cart component changes are visible in CartCounter component where I show it. I don't wanna use Redux, but I know that is possible with Context Api. I did a long search to find an example similar to mine but I didn't find it. Can you show me how to use it in my case?
Here my code
Cart Component
export default class Cart extends React.Component {
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerTransparent: true,
headerTintColor: "black",
headerRight: () => {
return <CartCounter cartTotal={this.state.cartTotal} />
}
};
}
constructor (props) {
super(props);
this.state = {
cartTotal: 0
}
}
...
...
...
CartCounter Component
export default class CartCounter extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<TouchableOpacity onPress={ this.onPress }>
<View style={ styles.root }>
<Text style={ styles.total }>{ cart total updates I want to see } </Text>
</View>
</TouchableOpacity>
)
}
}
...
...
...
Thanks in advance
use react context https://reactjs.org/docs/context.html to create a global state and seter

passing array state to another class react native

I am new to React Native. I am making a lunch picker app for practice, and I wonder how to pass array of state to another class.
What I want to do is: show all data of array in child screen, so users can check the data by clicking on a button.
However, it seems like my code returns null array instead of getting original array. All codes are in one file, App.js
Home screen
class HomeScreen extends React.Component {
//initial
constructor(props) {
super(props);
this.state = {
isReady: false,
myMenu: '????',
menutext: '',
randomArray: ['a', 'b', 'c'],
};
}
render() {
return (
<View style={[styles.mainContainer]}>
<DetailsScreen menuListAA={this.state.randomArray} />
<Button
label="Show all menu"
onPress={() => {
/* 1. Navigate to the menu page route with params */
this.props.navigation.navigate('Details', {
itemId: 0,
otherParam: 'Show all menu',
});
}}
/>
</View>
Details screen
class DetailsScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
static navigationOptions = {
title: 'All menu',
};
loadMenuList() {
const allMenuList = this.props.menuListAA;
return allMenuList.map((item, index) => <Text key={index}>{item}</Text>);
}
render() {
return (
<ScrollView>
<View style={styles.row}>{this.props.menuListAA}</View>
<Button
label="Go back"
onPress={() => this.props.navigation.goBack()}
/>
</ScrollView>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
Let me know if you need more information.
Please replace your code with one below :
Home screen
class HomeScreen extends React.Component {
//initial
constructor(props) {
super(props);
this.state = {
isReady: false,
myMenu: '????',
menutext: '',
randomArray: ['a', 'b', 'c'],
};
}
render() {
return (
<View style={[styles.mainContainer]}>
<DetailsScreen menuListAA={this.state.randomArray} />
<Button
label="Show all menu"
onPress={() => {
/* 1. Navigate to the menu page route with params */
this.props.navigation.navigate('Details', {
itemId: 0,
otherParam: 'Show all menu',
});
}}
/>
</View>
Doubts Detail : replace View with Text component
class DetailsScreen extends React.Component {
constructor(props) {
super(props);
this.state = {
}
}
static navigationOptions = {
title: 'All menu',
};
loadMenuList() {
const allMenuList = this.props.menuListAA;
return allMenuList.map((item, index) => <Text key={index}>{item}</Text>);
}
render() {
return (
<ScrollView>
//change View with Text as view cannot render characters and error says the same
<Text style={styles.row}>{this.props.menuListAA}</Text>
<Button
label="Go back"
onPress={() => this.props.navigation.goBack()}
/>
</ScrollView>
);
}
}
const RootStack = createStackNavigator(
{
Home: HomeScreen,
Details: DetailsScreen,
},
{
initialRouteName: 'Home',
}
);
const AppContainer = createAppContainer(RootStack);
export default class App extends React.Component {
render() {
return <AppContainer />;
}
}
you can pass data from parent to child by set props for child component like this: <Child data={this.state.yourData} /> //the props name is data,
then in child component in componentWillMount() you can access it:
ComponentWillMount(){
this.setState({childData: this.props.data}) // childData is an state of child component,rename it to your own`
}
in some case data maybe changed so we must to check this in componentDidUpdate() :
componentDidUpdate(){
if(this.props.data != this.state.childData){ //it is necessary because this function called many time's
this.setState({childData: this.props.data})
}
if you was in parent & want to give any data from child you must pass a function that returned data as like below:
<Child getData={(value)=> console.log(value)} />
then in your child you can do this:
this.props.getData(anyDataYouWantGetInParent)
hope this answer will be helpful,let me know any question
good luck.

How change a state variable via props?

I'm trying do show a modal screen with a button, when i press the button the modal will appears. The variable that is responsable for it is modalVisible and its a variable state. But if my button is not in the same class of my screen Profile i'm not able to change the variable, at last i dont know a way to do:
class Carrousel extends React.Component {
render () {
return (
<Button
title= 'press me to show up'
onPress= {() => this.props.visible = true}
/>
);
}
}
export default class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible: true,
};
}
render() {
return (
<View>
<Modal
visible = {this.state.modalVisible}>
</Modal>
<Carrousel visible = {this.state.modalVisible}/>
</View>
);
}
}
Is there an way to change this variable modalVisible in another class? Or is there another way to i show the modal with a button in another class?
*The carrousel class is another file Carrousel.js.
You have to set a function as a prop that changes the state on the Parent Component. I haven't tried but it should be something like this:
class Carrousel extends React.Component {
render () {
return (
<Button
title= 'press me to show up'
onPress= {() => this.props.setVisible()}
/>
);
}
}
export default class Profile extends React.Component {
constructor(props) {
super(props);
this.state = {
modalVisible: false,
};
this.setVisible = this.setVisible.bind(this);
}
setVisible() {
this.setState({
modalVisible: true,
})
}
render() {
return (
<View>
<Modal
visible = {this.state.modalVisible}>
</Modal>
<Carrousel setVisible={this.setVisible}/>
</View>
);
}
}

Update value in StackNavigation custom header

I defined a custom view as a component for headerRight property in navigationOptions as bellow:
static navigationOptions = ({ navigation }) => {
return {
headerRight: navigation.getParam('headerRight', null),
};
};
and then in componentDidMount:
this.props.navigation.setParams({
headerRight: (<MessageDetailsHeader {...this.props}
title = {this.state.headerTitle}
subTitle = {this.state.headerSubTitle}
online = {this.state.online}
/>)
})
also i defined some state for updating values:
constructor(props) {
super(props);
this.state = {
headerTitle: null,
headerSubTitle: null,
headerOnline: false
};
}
Custom header view component defined as bellow:
export default class MessageDetailsHeader extends React.Component {
constructor(props) {
super(props);
this.state = {
title: this.props.title,
subTitle: this.props.subTitle,
online: this.props.online
};
}
componentWillReceiveProps(nextProps) {
this.setState({
title: nextProps.title,
subTitle: nextProps.subTitle,
online: nextProps.online,
})
}
render() {
return (
<View style={styles.headerContainer}>
<View style={styles.headerDetailsContainer}>
<Text style={styles.headerTitle}>{this.state.title}</Text>
<Text style={styles.headerSubTitle}>{this.state.subTitle}</Text>
</View>
<Avatar small rounded source={require('../images/no-profile.png')} activeOpacity={0.7} avatarStyle={this.state.online? styles.avatarOnline: styles.avatarOffline}/>
</View>
);
}
}
I need to call setState in screen and then update Custom View in navigation header, is this a right way?
Thanks in advance
Solved!
static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
headerRight: <MessageDetailsHeader
title={params.headerTitle}
subTitle={params.headerSubTitle}
online={params.headerOnline}
/>
};
};
and then called bellow code to set new value, easily!
this.props.navigation.setParams({
headerSubTitle: 'online',
});

How would I disable a function by clicking on TextInput

I have this class for blinking text:
class BlinkerFluid extends React.Component {
constructor(props) {
super(props);
this.state = {
isShowingText: true,
TextInputName = ''
};
setInterval(() => {
this.setState(previousState => {
return { isShowingText: !previousState.isShowingText };
});
}, 700);
}
render () {
let display = this.state.isShowingText ? this.props.text : ' ';
return (
<Text style={styles.other}>{display}</Text>
);
}
}
And this class to render the blinking text:
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { text: 'x' };
}
render() {
return (
<View style={styles.container}>
<Text style={styles.other}>$ root | > </Text>
<BlinkerFluid text=' _' />
<TextInput style={styles.lol}
onChangeText={(text) => this.setState({text})}
value={this.state.text}
/>
</View>
);
}
}
I want to be able to stop the blinking of the text when I click on the TextInput prop and/or write to it.
How would I go about doing that?
I've been trying to search for an answer on my own, read the docs, but nothing has helped.
Any help is appreciated!
You could try something like the below code snippet (I've only included the code for App. You can implement the paused functionality in the BlinkerFluid however you want.
Basically, I'm storing in the state whether the blinker should be paused or not, then passing that value through to the BlinkerFluid component as a prop. You can set the pauseBlinker state easily, perhaps on onChangeText as in the example, or on press of a TouchableOpacity element etc.
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
text: 'x',
pauseBlinker: false,
};
}
render() {
return (
<View style={styles.container}>
<Text style={styles.other}>$ root | > </Text>
<BlinkerFluid text=' _' paused={this.state.pauseBlinker} />
<TextInput style={styles.lol}
onChangeText={(text) => this.setState({ text, pauseBlinker: true, })}
value={this.state.text}
/>
</View>
);
}
}