admin管理员组文章数量:1425932
I am developing a small React app, I'm having Files like
--Filter.js
--Result.js
--Index.js
Filter.js - Contains 2 textbox and button
Result.js - It contains the Filter.js and one P tag.
Index.js - Contains the Result.js
Filter.js:
import React, { Component } from 'react';
import { render } from 'react-dom';
export default class Filter extends Component {
constructor(props){
super(props);
this.state={myName: ""}
}
SubmitValue = (e) => {
this.props.handleData(this.state.myName)
}
onChange=(e)=>{
this.setState({myName: e.target.value})
}
render() {
return (
<div>
<form>
Name: <input type="text" name="myName" onChange={this.onChange}/>
<br />
Email: <input type="text" name="myEmail" />
<br /><br />
<input type="button" value="Submit" onClick={this.SubmitValue}/>
</form>
</div>
)
}
}
ResultList.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Filter from './Filter';
import { Button, Dropdown, Input, } from 'semantic-ui-react';
export default class ResultList extends Component {
constructor(props){
super(props);
this.state = {
myName: ''
};
}
handleParentData = (e) => {
this.setState({myName: e})
}
render() {
return (
<div>
<Filter handleData={this.handleParentData}/>
<p>{this.state.myName}</p>
</div>
)
}
}
Index.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
import ResultList from './Components/ResultList';
class App extends Component {
render() {
return (
<div>
<ResultList />
</div>
);
}
}
render(<App />, document.getElementById('root'));
I want the Filter.js textbox values in Result.js. I can able to get one text field value to Result.js. But unable to get the both text fields values. Code link:
How to get the both text field values in Result.js?
I am developing a small React app, I'm having Files like
--Filter.js
--Result.js
--Index.js
Filter.js - Contains 2 textbox and button
Result.js - It contains the Filter.js and one P tag.
Index.js - Contains the Result.js
Filter.js:
import React, { Component } from 'react';
import { render } from 'react-dom';
export default class Filter extends Component {
constructor(props){
super(props);
this.state={myName: ""}
}
SubmitValue = (e) => {
this.props.handleData(this.state.myName)
}
onChange=(e)=>{
this.setState({myName: e.target.value})
}
render() {
return (
<div>
<form>
Name: <input type="text" name="myName" onChange={this.onChange}/>
<br />
Email: <input type="text" name="myEmail" />
<br /><br />
<input type="button" value="Submit" onClick={this.SubmitValue}/>
</form>
</div>
)
}
}
ResultList.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Filter from './Filter';
import { Button, Dropdown, Input, } from 'semantic-ui-react';
export default class ResultList extends Component {
constructor(props){
super(props);
this.state = {
myName: ''
};
}
handleParentData = (e) => {
this.setState({myName: e})
}
render() {
return (
<div>
<Filter handleData={this.handleParentData}/>
<p>{this.state.myName}</p>
</div>
)
}
}
Index.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';
import ResultList from './Components/ResultList';
class App extends Component {
render() {
return (
<div>
<ResultList />
</div>
);
}
}
render(<App />, document.getElementById('root'));
I want the Filter.js textbox values in Result.js. I can able to get one text field value to Result.js. But unable to get the both text fields values. Code link: https://stackblitz./edit/dhanapal-react-ex-1
How to get the both text field values in Result.js?
Share Improve this question edited Feb 11, 2019 at 17:06 Dhanapal asked Feb 11, 2019 at 16:50 DhanapalDhanapal 3802 gold badges8 silver badges30 bronze badges 4- No one will visit that link, which will be dead in an internet moment anyway, making this question useless for future readers. Provide a minimal reproducible example as suggested in How to Ask. – user1531971 Commented Feb 11, 2019 at 17:02
- Added the code in the question. – Dhanapal Commented Feb 11, 2019 at 17:07
- Did you check on SO already? stackoverflow./q/38420396/1531971 among others and see if that helps. – user1531971 Commented Feb 11, 2019 at 17:11
- I looked few references but that is not helped me. I am unable to get both values – Dhanapal Commented Feb 11, 2019 at 17:15
1 Answer
Reset to default 1ok, so I made some changes to your code that you can view here: https://stackblitz./edit/dhanapal-react-ex-1-8pzeks?file=index.js
In the case that the above link no longer works, here is what I changed: Filter.Js:
export default class Filter extends Component {
constructor(props){
super(props);
this.state = {
myName: ''
}
}
submitForm = (e) => {
e.preventDefault();
this.props.handleData(this.state)
}
onChange = (e) => {
this.setState({
myName: e.target.value
});
}
render() {
return (
<div>
<form>
Name: <input type="text" name="myName" onChange={this.onChange}/>
<br /><br />
<input type="button" value="Submit" onClick={this.submitForm}/>
</form>
</div>
)
}
}
and in ResultList.js:
export default class ResultList extends Component {
constructor(props){
super(props);
this.state = {
myName: ''
};
}
handleParentData = (formModel) => {
this.setState({...formModel});
}
render() {
return (
<div>
<Filter handleData={this.handleParentData}/>
<p>{this.state.myName}</p>
</div>
)
}
}
It's important to note that any object you create in JS (i.e. your state in each ponent) is already in JSON format so no need to try and convert it. Here is what the problem was:
In Filter.js
, you were passing a single property from the state (this.state.myName
) and in ResultList.js
, function handleParentData()
you were expecting to receive an event much like you were from the on change functions but you were really just passing a string. That's why nothing would happen. In Filter.js
when you want to post back the form to the parent ponent, I would suggest passing back the entire state object (which is already a JSON object) instead of an individual property. Refer to my working example and you should be able to see what I mean.
P.S. I think I accidentally edited your original code example, if I did I'm terribly sorry I didn't mean to.
本文标签: javascriptReactpass form data from one component to anotherStack Overflow
版权声明:本文标题:javascript - React | pass form data from one component to another - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745456364a2659120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论