get details of form through props when submitted
I just started development with react native and i'm having difficulty getting the value of an input field through props.
Parent
class HomeContainerScreen extends Component {
state = {
userName:'',
passWord:''
}
userLogin=()=>{
alert(this.state.userName);
}
render() {
return (
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
/>
);
}
}
export default HomeContainerScreen;
Child
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,Image} from 'react-native';
import { Container, Content, Form, Input, Item, Label, Button} from 'native-base'
class HomeScreen extends Component {
render() {
return (
<Container>
<Content contentContainerStyle={{alignItems:'center', justifyContent:'center',top:150}}>
<Image style={{height:200, width:200, alignSelf:"center"}} source={require('../../assets/imgs/logo.jpg')}/>
</Content>
<Content>
<Form>
<Item stackedLabel>
<Label>Username</Label>
<Input onChangeText={(text=>this.setState({userName:this.props.userName}))}/>
</Item>
<Item stackedLabel last>
<Label>Password</Label>
<Input onChangeText={(text=>this.setState({passWord:this.props.passWord}))} />
</Item>
<Button
onPress={()=>this.props.userLogin()}
style={{width:'80%', alignSelf:'center',margin:20}} iconLeft block>
<Text style={{color:'white', fontSize:16}}>Sign In</Text>
</Button>
</Form>
</Content>
</Container>
);
}
}
export default HomeScreen
i've set it to get the value when the form is submitted, but on submit anything happens.
Please any help on how to get it working.
reactjs react-native
add a comment |
I just started development with react native and i'm having difficulty getting the value of an input field through props.
Parent
class HomeContainerScreen extends Component {
state = {
userName:'',
passWord:''
}
userLogin=()=>{
alert(this.state.userName);
}
render() {
return (
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
/>
);
}
}
export default HomeContainerScreen;
Child
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,Image} from 'react-native';
import { Container, Content, Form, Input, Item, Label, Button} from 'native-base'
class HomeScreen extends Component {
render() {
return (
<Container>
<Content contentContainerStyle={{alignItems:'center', justifyContent:'center',top:150}}>
<Image style={{height:200, width:200, alignSelf:"center"}} source={require('../../assets/imgs/logo.jpg')}/>
</Content>
<Content>
<Form>
<Item stackedLabel>
<Label>Username</Label>
<Input onChangeText={(text=>this.setState({userName:this.props.userName}))}/>
</Item>
<Item stackedLabel last>
<Label>Password</Label>
<Input onChangeText={(text=>this.setState({passWord:this.props.passWord}))} />
</Item>
<Button
onPress={()=>this.props.userLogin()}
style={{width:'80%', alignSelf:'center',margin:20}} iconLeft block>
<Text style={{color:'white', fontSize:16}}>Sign In</Text>
</Button>
</Form>
</Content>
</Container>
);
}
}
export default HomeScreen
i've set it to get the value when the form is submitted, but on submit anything happens.
Please any help on how to get it working.
reactjs react-native
add a comment |
I just started development with react native and i'm having difficulty getting the value of an input field through props.
Parent
class HomeContainerScreen extends Component {
state = {
userName:'',
passWord:''
}
userLogin=()=>{
alert(this.state.userName);
}
render() {
return (
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
/>
);
}
}
export default HomeContainerScreen;
Child
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,Image} from 'react-native';
import { Container, Content, Form, Input, Item, Label, Button} from 'native-base'
class HomeScreen extends Component {
render() {
return (
<Container>
<Content contentContainerStyle={{alignItems:'center', justifyContent:'center',top:150}}>
<Image style={{height:200, width:200, alignSelf:"center"}} source={require('../../assets/imgs/logo.jpg')}/>
</Content>
<Content>
<Form>
<Item stackedLabel>
<Label>Username</Label>
<Input onChangeText={(text=>this.setState({userName:this.props.userName}))}/>
</Item>
<Item stackedLabel last>
<Label>Password</Label>
<Input onChangeText={(text=>this.setState({passWord:this.props.passWord}))} />
</Item>
<Button
onPress={()=>this.props.userLogin()}
style={{width:'80%', alignSelf:'center',margin:20}} iconLeft block>
<Text style={{color:'white', fontSize:16}}>Sign In</Text>
</Button>
</Form>
</Content>
</Container>
);
}
}
export default HomeScreen
i've set it to get the value when the form is submitted, but on submit anything happens.
Please any help on how to get it working.
reactjs react-native
I just started development with react native and i'm having difficulty getting the value of an input field through props.
Parent
class HomeContainerScreen extends Component {
state = {
userName:'',
passWord:''
}
userLogin=()=>{
alert(this.state.userName);
}
render() {
return (
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
/>
);
}
}
export default HomeContainerScreen;
Child
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View,Image} from 'react-native';
import { Container, Content, Form, Input, Item, Label, Button} from 'native-base'
class HomeScreen extends Component {
render() {
return (
<Container>
<Content contentContainerStyle={{alignItems:'center', justifyContent:'center',top:150}}>
<Image style={{height:200, width:200, alignSelf:"center"}} source={require('../../assets/imgs/logo.jpg')}/>
</Content>
<Content>
<Form>
<Item stackedLabel>
<Label>Username</Label>
<Input onChangeText={(text=>this.setState({userName:this.props.userName}))}/>
</Item>
<Item stackedLabel last>
<Label>Password</Label>
<Input onChangeText={(text=>this.setState({passWord:this.props.passWord}))} />
</Item>
<Button
onPress={()=>this.props.userLogin()}
style={{width:'80%', alignSelf:'center',margin:20}} iconLeft block>
<Text style={{color:'white', fontSize:16}}>Sign In</Text>
</Button>
</Form>
</Content>
</Container>
);
}
}
export default HomeScreen
i've set it to get the value when the form is submitted, but on submit anything happens.
Please any help on how to get it working.
reactjs react-native
reactjs react-native
asked Nov 23 '18 at 16:19
developerdeveloper
607
607
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You're setting the state of your child component, not the parent. You could pass another callback function as a prop, so that the call to this.setState happens within your parent component, not the child.
Parent:
setUserName = (userName) => {
this.setState({ userName: userName })
}
setPassWord= (passWord) => {
this.setState({ passWord: passWord})
}
inside render():
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
setUserName = {this.setUserName}
setPassWord = {this.setPassWord}
/>
and in child:
<Input onChangeText={this.props.setUserName}/>
...
<Input onChangeText={this.props.setPassWord}/>
Also, this call to setState:
this.setState({ userName: this.props.userName })
Won't do much as it just sets the state to the same value as it already has. You probably want to set it to the new value.
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450001%2fget-details-of-form-through-props-when-submitted%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
You're setting the state of your child component, not the parent. You could pass another callback function as a prop, so that the call to this.setState happens within your parent component, not the child.
Parent:
setUserName = (userName) => {
this.setState({ userName: userName })
}
setPassWord= (passWord) => {
this.setState({ passWord: passWord})
}
inside render():
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
setUserName = {this.setUserName}
setPassWord = {this.setPassWord}
/>
and in child:
<Input onChangeText={this.props.setUserName}/>
...
<Input onChangeText={this.props.setPassWord}/>
Also, this call to setState:
this.setState({ userName: this.props.userName })
Won't do much as it just sets the state to the same value as it already has. You probably want to set it to the new value.
add a comment |
You're setting the state of your child component, not the parent. You could pass another callback function as a prop, so that the call to this.setState happens within your parent component, not the child.
Parent:
setUserName = (userName) => {
this.setState({ userName: userName })
}
setPassWord= (passWord) => {
this.setState({ passWord: passWord})
}
inside render():
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
setUserName = {this.setUserName}
setPassWord = {this.setPassWord}
/>
and in child:
<Input onChangeText={this.props.setUserName}/>
...
<Input onChangeText={this.props.setPassWord}/>
Also, this call to setState:
this.setState({ userName: this.props.userName })
Won't do much as it just sets the state to the same value as it already has. You probably want to set it to the new value.
add a comment |
You're setting the state of your child component, not the parent. You could pass another callback function as a prop, so that the call to this.setState happens within your parent component, not the child.
Parent:
setUserName = (userName) => {
this.setState({ userName: userName })
}
setPassWord= (passWord) => {
this.setState({ passWord: passWord})
}
inside render():
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
setUserName = {this.setUserName}
setPassWord = {this.setPassWord}
/>
and in child:
<Input onChangeText={this.props.setUserName}/>
...
<Input onChangeText={this.props.setPassWord}/>
Also, this call to setState:
this.setState({ userName: this.props.userName })
Won't do much as it just sets the state to the same value as it already has. You probably want to set it to the new value.
You're setting the state of your child component, not the parent. You could pass another callback function as a prop, so that the call to this.setState happens within your parent component, not the child.
Parent:
setUserName = (userName) => {
this.setState({ userName: userName })
}
setPassWord= (passWord) => {
this.setState({ passWord: passWord})
}
inside render():
<HomeScreen
userLogin = {this.userLogin}
userName = {this.state.userName}
passWord = {this.state.passWord}
setUserName = {this.setUserName}
setPassWord = {this.setPassWord}
/>
and in child:
<Input onChangeText={this.props.setUserName}/>
...
<Input onChangeText={this.props.setPassWord}/>
Also, this call to setState:
this.setState({ userName: this.props.userName })
Won't do much as it just sets the state to the same value as it already has. You probably want to set it to the new value.
answered Nov 23 '18 at 16:46
Viktor WViktor W
564
564
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450001%2fget-details-of-form-through-props-when-submitted%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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