0

I am using an array to implement this. To add, I'm pushing a view in the array and updating the array state. But I'm not sure how to delete it, array.pop() is not working. And even it it does, it would only remove the last element of the array. I want to use something like splice(), where i can pass an index, to delete the view whose "delete" button was invoked. But not sure how to implement this. Any suggestions? I'm adding the code for my add and delete methods. I wanna delete the view whose delete button was pressed. Please help!

enter image description here

enter image description here

Thanks in advance

2

2 Answers 2

3

you don't need to manipulate view by your self, you need to manage your data structure in a shape that you could iterate over it and render the proper component of your desire.

for handling the finding an item from array and deleting it, i would suggest you that to have an ID for each of your array item, so this means that you need to convert them to array of object and each object should have a key of id some thing like below:

const myData = [
  { id: 1, val: 'some value' },
  { id: 2, val: 'some other value' },
  // more ...
]

by doing this your id becomes the key to find items by id, filter items by id and do what ever you want to that result, and by setting the resulted array of objects in your state, your return part of component would use state and since state is always gets updated after any operation you made to it, then you will see that result is being updated automatically!

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

Comments

0

How to dynamically add/delete form in react/react-native First create a state array which will represent your form structure, now it can be fetched your any API, or you can create it manually

this.state = {
        array: [{
            studentName: 'John doe',
            uid: 1
        }]
    }

Loop over the same state array

{
                    array.map((item, index) => {
                        return (
                            <View style={{ margin: 5, padding: 10, }}>
                                <TextInput placeholder='Student name'
                                    value={item.studentName}
                                    style={{ borderRadius: 4, borderWidth: 1, borderColor: '#212121', padding: 8 }}
                                    onChangeText={text => {
                                        this.setState({ array: array.map((c, innerIndex) => innerIndex === index ? { ...c, studentName: text } : c) })
                                    }} />
                                <TextInput placeholder='Student UID'
                                    value={item.uid}
                                    style={{ borderRadius: 4, borderWidth: 1, borderColor: '#212121', padding: 8, marginTop: 5 }}
                                    onChangeText={text => {
                                        this.setState({ array: array.map((c, innerIndex) => innerIndex === index ? { ...c, uid: text } : c) })
                                    }} />
                                <Text style={{
                                    alignSelf: 'flex-start', borderRadius: 4,
                                    padding: 3, marginTop: 5, backgroundColor: 'red', color: 'white'
                                }} onPress={() => {
                                    const filterList = array.filter((item, inner) => inner != index)
                                    this.setState({ array: filterList })
                                }}>
                                    Remove
                                </Text>
                            </View>
                        )
                    })
                }

When you want to add one more field create a handler, which will simply add an empty object to your state array, as you have already looped over state array so when ever state array changes your form changes

<Text style={{ padding: 16, textAlign: 'center', backgroundColor: 'black', color: 'white' }} onPress={() => {
                    this.setState({
                        array: [...array, {
                            studentName: '',
                            uid: ''
                        }]
                    })
                }}>
                    Add
                </Text>

Complete App.js code is available here

Comments

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.