Changing text on button in multiple items (React.js)
up vote
0
down vote
favorite
I'm new to using React. I have a series of data in which there is a button in every item and by clicking it the text of a special div must be changed, but the issue is that on clicking the button of one item, the text of the div in all items is changed. I want that when I click the button, the text of the div which is in the respected item should change.The main problem is that when second item clicked the first item text will disappear and the text of second one is the text of first one.Thank you in advance.
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
data: ,
num: 0,
};
$.ajax({
url: "/json.bc",
type: "get",
success: (result) => {
this.setState({ data: eval(result) });
}
})
}
renderFlight() {
return this.state.data.map((item, i) => {
return (
<div class="item">
<div class="btn" onClick={((e) => this.rueldep(e, item, i))}>click</div>
<div class="text">
{(this.state.num == i + 1) ? this.state.rueldepTxt : null}
</div>
</div>
)
})
}
rueldep = (e, item, index) => {
var desc = item.info.departureinformation.routeshow.description
return this.setState({
rueldepTxt: desc,
num: index + 1
});
};
render() {
return (
<div>{this.renderFlight()}</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('Result'));
reactjs
|
show 5 more comments
up vote
0
down vote
favorite
I'm new to using React. I have a series of data in which there is a button in every item and by clicking it the text of a special div must be changed, but the issue is that on clicking the button of one item, the text of the div in all items is changed. I want that when I click the button, the text of the div which is in the respected item should change.The main problem is that when second item clicked the first item text will disappear and the text of second one is the text of first one.Thank you in advance.
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
data: ,
num: 0,
};
$.ajax({
url: "/json.bc",
type: "get",
success: (result) => {
this.setState({ data: eval(result) });
}
})
}
renderFlight() {
return this.state.data.map((item, i) => {
return (
<div class="item">
<div class="btn" onClick={((e) => this.rueldep(e, item, i))}>click</div>
<div class="text">
{(this.state.num == i + 1) ? this.state.rueldepTxt : null}
</div>
</div>
)
})
}
rueldep = (e, item, index) => {
var desc = item.info.departureinformation.routeshow.description
return this.setState({
rueldepTxt: desc,
num: index + 1
});
};
render() {
return (
<div>{this.renderFlight()}</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('Result'));
reactjs
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44
|
show 5 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm new to using React. I have a series of data in which there is a button in every item and by clicking it the text of a special div must be changed, but the issue is that on clicking the button of one item, the text of the div in all items is changed. I want that when I click the button, the text of the div which is in the respected item should change.The main problem is that when second item clicked the first item text will disappear and the text of second one is the text of first one.Thank you in advance.
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
data: ,
num: 0,
};
$.ajax({
url: "/json.bc",
type: "get",
success: (result) => {
this.setState({ data: eval(result) });
}
})
}
renderFlight() {
return this.state.data.map((item, i) => {
return (
<div class="item">
<div class="btn" onClick={((e) => this.rueldep(e, item, i))}>click</div>
<div class="text">
{(this.state.num == i + 1) ? this.state.rueldepTxt : null}
</div>
</div>
)
})
}
rueldep = (e, item, index) => {
var desc = item.info.departureinformation.routeshow.description
return this.setState({
rueldepTxt: desc,
num: index + 1
});
};
render() {
return (
<div>{this.renderFlight()}</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('Result'));
reactjs
I'm new to using React. I have a series of data in which there is a button in every item and by clicking it the text of a special div must be changed, but the issue is that on clicking the button of one item, the text of the div in all items is changed. I want that when I click the button, the text of the div which is in the respected item should change.The main problem is that when second item clicked the first item text will disappear and the text of second one is the text of first one.Thank you in advance.
class App extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
data: ,
num: 0,
};
$.ajax({
url: "/json.bc",
type: "get",
success: (result) => {
this.setState({ data: eval(result) });
}
})
}
renderFlight() {
return this.state.data.map((item, i) => {
return (
<div class="item">
<div class="btn" onClick={((e) => this.rueldep(e, item, i))}>click</div>
<div class="text">
{(this.state.num == i + 1) ? this.state.rueldepTxt : null}
</div>
</div>
)
})
}
rueldep = (e, item, index) => {
var desc = item.info.departureinformation.routeshow.description
return this.setState({
rueldepTxt: desc,
num: index + 1
});
};
render() {
return (
<div>{this.renderFlight()}</div>
)
}
}
ReactDOM.render(<App />, document.getElementById('Result'));
reactjs
reactjs
edited Nov 20 at 8:36
asked Nov 20 at 7:17
bita
136
136
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44
|
show 5 more comments
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44
|
show 5 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53388021%2fchanging-text-on-button-in-multiple-items-react-js%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
You don't need to return anything in map. return this.state.data.map((item,i)=>{ return( Remove the second return bracket
– Nagesh Katna
Nov 20 at 7:30
Hi Nagesh Katna.I didn't get what you mean. I should remove (e,item,i) in this.rueldep(e,item,i) ??
– bita
Nov 20 at 7:39
So what would be the difference in not using it?
– bita
Nov 20 at 7:41
No, I mean that you have two return statements in your renderFlight() function. The second return statement is not needed.
– Nagesh Katna
Nov 20 at 7:41
Can you give an example of what kind of data are you receiving in your state 'data'
– Nagesh Katna
Nov 20 at 7:44