Skip to content

Splice(remove) method works exactly opposite of its actual work #26304

Description

@Robiullah2244

I am expecting to only remove the given index in "deleteNumber" method . But Its doing exactly opposite. It removes all indexes except given index.

deleteNumber = (index) => {
    console.log("index: ", index);
    this.setState((state) => {
      const numbers = state.numbers.splice(index, 1);
      return(
        {
          numbers: numbers,
        }
      )
    })
  }

  render()
  {
    const numbers = this.state.numbers.map((number, index) => (
      
      <View style={{width: "100%", padding: 10, backgroundColor: "#eee", margin: 5}} key={index}>
        <Text><Icon size={25} name='ios-trash' color="red" onPress={()=> this.deleteNumber(index)}></Icon> {number}</Text>
      </View>
      
    ))

    return (
      <View style={{flex: 1,flexDirection:"column"}}>
        <Text style={{textAlign:"center", color: "#FBEAEB", fontSize: 50, backgroundColor:"#2F3C7E"}}>Security Notifier</Text>
        <View style={{
          flex: 1,
          flexDirection: 'column',
          justifyContent: 'center',
        }}>
          <View style={{padding: 20}}>
            <TextInput 
                style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
                onChangeText={(newNumber) => this.setState({ newNumber })}
                placeholder="Enter Phone Number"
                defaultValue=""
                value={this.state.newNumber}
            />
            <View style={{paddingTop: 5}}>
              <Button title="Add Number" onPress = {() => this.addNumber()}/>
            </View>
          </View>
          <View>
            {numbers}
          </View>
        </View>
      </View>
    );
  }

React Native version: 0.60.5
System:
OS: Windows 10
CPU: (6) x64 Intel(R) Core(TM) i5-8400 CPU @ 2.80GHz
Memory: 3.40 GB / 15.66 GB
Binaries:
Node: 10.16.3 - C:\Program Files\nodejs\node.EXE
Yarn: 1.17.3 - C:\Users\USER\AppData\Roaming\npm\yarn.CMD
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
IDEs:
Android Studio: Version 3.4.0.0 AI-183.6156.11.34.5692245

image
image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions