call function without onpress react native











up vote
1
down vote

favorite












I'm new to react native. I am trying to get 'Key' without using the onpress in button.
I just want to get a 'key', when i could open component. How it could be possible?



import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Button,
View,
AsyncStorage
} from 'react-native';

export default class History extends Component {
constructor(props) {
super(props);
this.state = {
myKey: null
}

}
async getKey() {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is = {this.state.myKey}
</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
padding: 30,
flex: 1,
backgroundColor: '#F5FCFF',
},
});


I am able to get a key with onpress but i want without onpress. Please suggest.










share|improve this question






















  • I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
    – kivul
    Nov 21 at 16:31

















up vote
1
down vote

favorite












I'm new to react native. I am trying to get 'Key' without using the onpress in button.
I just want to get a 'key', when i could open component. How it could be possible?



import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Button,
View,
AsyncStorage
} from 'react-native';

export default class History extends Component {
constructor(props) {
super(props);
this.state = {
myKey: null
}

}
async getKey() {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is = {this.state.myKey}
</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
padding: 30,
flex: 1,
backgroundColor: '#F5FCFF',
},
});


I am able to get a key with onpress but i want without onpress. Please suggest.










share|improve this question






















  • I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
    – kivul
    Nov 21 at 16:31















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm new to react native. I am trying to get 'Key' without using the onpress in button.
I just want to get a 'key', when i could open component. How it could be possible?



import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Button,
View,
AsyncStorage
} from 'react-native';

export default class History extends Component {
constructor(props) {
super(props);
this.state = {
myKey: null
}

}
async getKey() {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is = {this.state.myKey}
</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
padding: 30,
flex: 1,
backgroundColor: '#F5FCFF',
},
});


I am able to get a key with onpress but i want without onpress. Please suggest.










share|improve this question













I'm new to react native. I am trying to get 'Key' without using the onpress in button.
I just want to get a 'key', when i could open component. How it could be possible?



import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TextInput,
Button,
View,
AsyncStorage
} from 'react-native';

export default class History extends Component {
constructor(props) {
super(props);
this.state = {
myKey: null
}

}
async getKey() {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is = {this.state.myKey}
</Text>
</View>
)
}
}

const styles = StyleSheet.create({
container: {
padding: 30,
flex: 1,
backgroundColor: '#F5FCFF',
},
});


I am able to get a key with onpress but i want without onpress. Please suggest.







javascript reactjs react-native






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 16:24









Mohit Yadav

196




196












  • I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
    – kivul
    Nov 21 at 16:31




















  • I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
    – kivul
    Nov 21 at 16:31


















I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
– kivul
Nov 21 at 16:31






I believe you could call your function on componentDidMount(): componentDidMount() { this.getkey.bind(this) } You can see more info here: reactjs.org/docs/react-component.html#componentdidmount
– kivul
Nov 21 at 16:31














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










You can simply get your key value with componentDidMount. As you should know, when the App runs and comes to the current screen, there is a flow of methods will be called before and after rendering the render function. So ComponentDidMount comes after render function is called. So since you need to only display the key value, just follow below code.



constructor(props) {
super(props);
this.state = {
myKey:''
}
}

getKey = async() => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

componentDidMount(){
this.getKey();
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is {this.state.myKey}
</Text>
</View>
)
}


Whenever render function being called, in that time, still key value is not set. So, this.state.myKey would be just Stored key is. But after that, once the componentDidMount called, it sets the myKey value and change states. Which will trig render function to render everything again. That would show your key value within the text component eventually without touching any button.






share|improve this answer





















  • Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
    – Mohit Yadav
    Nov 22 at 15:23










  • You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
    – Samitha Nanayakkara
    Nov 22 at 15:48











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416437%2fcall-function-without-onpress-react-native%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










You can simply get your key value with componentDidMount. As you should know, when the App runs and comes to the current screen, there is a flow of methods will be called before and after rendering the render function. So ComponentDidMount comes after render function is called. So since you need to only display the key value, just follow below code.



constructor(props) {
super(props);
this.state = {
myKey:''
}
}

getKey = async() => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

componentDidMount(){
this.getKey();
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is {this.state.myKey}
</Text>
</View>
)
}


Whenever render function being called, in that time, still key value is not set. So, this.state.myKey would be just Stored key is. But after that, once the componentDidMount called, it sets the myKey value and change states. Which will trig render function to render everything again. That would show your key value within the text component eventually without touching any button.






share|improve this answer





















  • Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
    – Mohit Yadav
    Nov 22 at 15:23










  • You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
    – Samitha Nanayakkara
    Nov 22 at 15:48















up vote
1
down vote



accepted










You can simply get your key value with componentDidMount. As you should know, when the App runs and comes to the current screen, there is a flow of methods will be called before and after rendering the render function. So ComponentDidMount comes after render function is called. So since you need to only display the key value, just follow below code.



constructor(props) {
super(props);
this.state = {
myKey:''
}
}

getKey = async() => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

componentDidMount(){
this.getKey();
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is {this.state.myKey}
</Text>
</View>
)
}


Whenever render function being called, in that time, still key value is not set. So, this.state.myKey would be just Stored key is. But after that, once the componentDidMount called, it sets the myKey value and change states. Which will trig render function to render everything again. That would show your key value within the text component eventually without touching any button.






share|improve this answer





















  • Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
    – Mohit Yadav
    Nov 22 at 15:23










  • You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
    – Samitha Nanayakkara
    Nov 22 at 15:48













up vote
1
down vote



accepted







up vote
1
down vote



accepted






You can simply get your key value with componentDidMount. As you should know, when the App runs and comes to the current screen, there is a flow of methods will be called before and after rendering the render function. So ComponentDidMount comes after render function is called. So since you need to only display the key value, just follow below code.



constructor(props) {
super(props);
this.state = {
myKey:''
}
}

getKey = async() => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

componentDidMount(){
this.getKey();
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is {this.state.myKey}
</Text>
</View>
)
}


Whenever render function being called, in that time, still key value is not set. So, this.state.myKey would be just Stored key is. But after that, once the componentDidMount called, it sets the myKey value and change states. Which will trig render function to render everything again. That would show your key value within the text component eventually without touching any button.






share|improve this answer












You can simply get your key value with componentDidMount. As you should know, when the App runs and comes to the current screen, there is a flow of methods will be called before and after rendering the render function. So ComponentDidMount comes after render function is called. So since you need to only display the key value, just follow below code.



constructor(props) {
super(props);
this.state = {
myKey:''
}
}

getKey = async() => {
try {
const value = await AsyncStorage.getItem('@MySuperStore:key');
this.setState({ myKey: value });
} catch (error) {
console.log("Error retrieving data" + error);
}
}

componentDidMount(){
this.getKey();
}

render() {
return (
<View style={styles.container}>
<Button
style={styles.formButton}
onPress={this.getKey.bind(this)}
title="Get Key"
color="#2196f3"
accessibilityLabel="Get Key"
/>
<Text >
Stored key is {this.state.myKey}
</Text>
</View>
)
}


Whenever render function being called, in that time, still key value is not set. So, this.state.myKey would be just Stored key is. But after that, once the componentDidMount called, it sets the myKey value and change states. Which will trig render function to render everything again. That would show your key value within the text component eventually without touching any button.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 16:38









Samitha Nanayakkara

1,249315




1,249315












  • Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
    – Mohit Yadav
    Nov 22 at 15:23










  • You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
    – Samitha Nanayakkara
    Nov 22 at 15:48


















  • Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
    – Mohit Yadav
    Nov 22 at 15:23










  • You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
    – Samitha Nanayakkara
    Nov 22 at 15:48
















Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
– Mohit Yadav
Nov 22 at 15:23




Thanks! for giving an answer. And it works. Could you please suggest that how to call a function which is in another component to this current component.
– Mohit Yadav
Nov 22 at 15:23












You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
– Samitha Nanayakkara
Nov 22 at 15:48




You can simply call child’s component function from parent using a reference. stackoverflow.com/a/40308103/8599868
– Samitha Nanayakkara
Nov 22 at 15:48


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416437%2fcall-function-without-onpress-react-native%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Futebolista

Lallio

Jornalista