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'));









share|improve this question
























  • 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















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'));









share|improve this question
























  • 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













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'));









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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

















active

oldest

votes











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%2f53388021%2fchanging-text-on-button-in-multiple-items-react-js%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53388021%2fchanging-text-on-button-in-multiple-items-react-js%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

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft