admin管理员组文章数量:1314411
What I'm trying to do is insert a value from the old array that I have render to the new array.
For now it can render the data that I want perfectly but when I push it into new array it bee [id : undefined]
How do call ticketId that I render from the ListView to save into myState array?
class SeactionSeating extends React.Component {
state = { ticket: [], myState: [] };
ponentWillMount() {
const tempticket = [];
let i;
let k = 1;
let x;
for (i = 1; i <= 50; i++) {
k++;
if (i < 10) {
x = 'north';
} else if (i < 20) {
x = 'south';
} else if (i < 30) {
x = 'west';
} else if (i < 40) {
x = 'east';
}
tempticket.push({ ticketId: i, row: k, gate: x });
}
this.setState({ ticket: tempticket });
this.setState({ myState: [] });
//i deliberately leave mystate empty so that i can push new array later
}
render() {
const ticketTemp = () => {
this.state.myState.push({ id: ticketId });
console.log(this.state.myState);
};
return (
<Container>
<View style={styles.listViewTitlePanel}>
<Text> hello there</Text>
</View>
<Content>
<ScrollView>
<List>
{ this.state.ticket.map((item, i) => (
<ListItem
roundAvatar
key={i}
avatar={
<View >
<Text>{item.ticketId}</Text>
</View>
}
title={
<View>
<Text>ROW :{item.row}</Text>
</View>
}
subtitle={
<View>
<Text>GATE :{item.gate}</Text>
</View>
}
//call ticketTemp
onPress={ticketTemp}
/>
))
}
</List>
</ScrollView>
</Content>
</Container>
);
}
}
What I'm trying to do is insert a value from the old array that I have render to the new array.
For now it can render the data that I want perfectly but when I push it into new array it bee [id : undefined]
How do call ticketId that I render from the ListView to save into myState array?
class SeactionSeating extends React.Component {
state = { ticket: [], myState: [] };
ponentWillMount() {
const tempticket = [];
let i;
let k = 1;
let x;
for (i = 1; i <= 50; i++) {
k++;
if (i < 10) {
x = 'north';
} else if (i < 20) {
x = 'south';
} else if (i < 30) {
x = 'west';
} else if (i < 40) {
x = 'east';
}
tempticket.push({ ticketId: i, row: k, gate: x });
}
this.setState({ ticket: tempticket });
this.setState({ myState: [] });
//i deliberately leave mystate empty so that i can push new array later
}
render() {
const ticketTemp = () => {
this.state.myState.push({ id: ticketId });
console.log(this.state.myState);
};
return (
<Container>
<View style={styles.listViewTitlePanel}>
<Text> hello there</Text>
</View>
<Content>
<ScrollView>
<List>
{ this.state.ticket.map((item, i) => (
<ListItem
roundAvatar
key={i}
avatar={
<View >
<Text>{item.ticketId}</Text>
</View>
}
title={
<View>
<Text>ROW :{item.row}</Text>
</View>
}
subtitle={
<View>
<Text>GATE :{item.gate}</Text>
</View>
}
//call ticketTemp
onPress={ticketTemp}
/>
))
}
</List>
</ScrollView>
</Content>
</Container>
);
}
}
Share
Improve this question
edited Jul 12, 2018 at 21:43
Vahid Boreiri
3,4381 gold badge20 silver badges35 bronze badges
asked Sep 21, 2017 at 5:13
Zachary LordfordZachary Lordford
1463 gold badges5 silver badges17 bronze badges
2
-
have you checked
ticketId
have value or not inticketTemp
method? – Jigar Shah Commented Sep 21, 2017 at 5:23 - @JigarShah yes i have check it has a value in it – Zachary Lordford Commented Sep 21, 2017 at 7:52
2 Answers
Reset to default 3You should pass trackId
to your ticketTemp
function that it can push this value into the myState
array. I just did that. And I transferred the ticketTemp
function to the outside of render
function because a function that modify the state can not be inside of render
.
Your code must be like this:
class SeactionSeating extends React.Component {
state = { ticket: [], myState: [] };
ponentWillMount() {
const tempticket = [];
let i;
let k = 1;
let x;
for (i = 1; i <= 50; i++) {
k++;
if (i < 10) {
x = 'north';
} else if (i < 20) {
x = 'south';
} else if (i < 30) {
x = 'west';
} else if (i < 40) {
x = 'east';
}
tempticket.push({ ticketId: i, row: k, gate: x });
}
this.setState({ ticket: tempticket });
// this.setState({ myState: [] }); //this line must be removed
//i deliberately leave mystate empty so that i can push new array later
}
ticketTemp(ticketId) {
this.state.myState.push({ id: ticketId });
console.log(this.state.myState);
};
render() {
return (
<Container>
<View style={styles.listViewTitlePanel}>
<Text> hello there</Text>
</View>
<Content>
<ScrollView>
<List>
{ this.state.ticket.map((item, i) => (
<ListItem
roundAvatar
key={i}
avatar={
<View >
<Text>{item.ticketId}</Text>
</View>
}
title={
<View>
<Text>ROW :{item.row}</Text>
</View>
}
subtitle={
<View>
<Text>GATE :{item.gate}</Text>
</View>
}
//call ticketTemp
onPress={() => this.ticketTemp(item.ticketId)}
/>
))
}
</List>
</ScrollView>
</Content>
</Container>
);
}
}
i made some change now i run without error thank you
You can change the ticketTemp method and pass ticketID as an argument. Then change the onPress and call the ticketTemp with ticketID.
class SeactionSeating extends React.Component {
state = { ticket: [], myState: [] };
ponentWillMount() {
const tempticket = [];
let i;
let k = 1;
let x;
for (i = 1; i <= 50; i++) {
k++;
if (i < 10) {
x = 'north';
} else if (i < 20) {
x = 'south';
} else if (i < 30) {
x = 'west';
} else if (i < 40) {
x = 'east';
}
tempticket.push({ ticketId: i, row: k, gate: x });
}
this.setState({ ticket: tempticket });
this.setState({ myState: [] });
//i deliberately leave mystate empty so that i can push new array later
}
render() {
const ticketTemp = (ticketId) => {
this.state.myState.push({ id: ticketId });
console.log(this.state.myState);
};
return (
<Container>
<View style={styles.listViewTitlePanel}>
<Text> hello there</Text>
</View>
<Content>
<ScrollView>
<List>
{ this.state.ticket.map((item, i) => (
<ListItem
roundAvatar
key={i}
avatar={
<View >
<Text>{item.ticketId}</Text>
</View>
}
title={
<View>
<Text>ROW :{item.row}</Text>
</View>
}
subtitle={
<View>
<Text>GATE :{item.gate}</Text>
</View>
}
//call ticketTemp
onPress={() => this.ticketTemp(item.ticketId)}
/>
))
}
</List>
</ScrollView>
</Content>
</Container>
);
}
}
本文标签: javascriptReactnative insert a value from render array to new arrayStack Overflow
版权声明:本文标题:javascript - React-native insert a value from render array to new array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741852454a2401145.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论