1

I am creating an Add button and cards every one of them contain three inputs. i want on user click a new MultiCityCard added and how to manage their states like the screen shot.

enter image description here

<View style={{}}>
    <MultiCityCard title="Flight 1" />
    <MultiCityCard title="Flignt 2" />
    <Button
      icon={{
        name: 'plus',
        size: 20,
        color: 'white',
        type: 'entypo',
      }}
      title="Add another flight"
      containerStyle={{ padding: 20 }}
    />
  </View>
3
  • Hello, you can check this question stackoverflow.com/questions/36651583/… However, you might need to lift up your state Commented Mar 4, 2019 at 14:08
  • @mliki brother can you use FlatList for such a scenario! I think that would be very much fine in a listing scenario! want me to give an example on that one? Commented Mar 4, 2019 at 14:33
  • @Rizwanatta sorry i couldn't understant your idea by using a flatList. can you give an exemple? Commented Mar 4, 2019 at 14:52

1 Answer 1

2

Take your data inside array. For example

data: [
  {
   from: '',
   to: '',
  }
]

and when you click on add, push new element to this array. for example

this.setState({ data: [{ from: '', to: ''},...this.state.data]})

Then you can edit or view the data according to index. For example

data.map((item, index) => <Button onPress={() => {
// here you have access to the index 
} }
/>);

If you want more detailed example, I posted a snack for you https://snack.expo.io/@azaabudeen/Y2FyZW

Sign up to request clarification or add additional context in comments.

2 Comments

yeah i get the idea but i will appreciate a detailed example.
Sure. Let me give you an example via snack

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.