Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Robiullah2244 opened this issue Sep 3, 2019 · 2 comments
Closed

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

Robiullah2244 opened this issue Sep 3, 2019 · 2 comments
Labels
Bug Resolution: Locked This issue was locked by the bot.

Comments

@Robiullah2244
Copy link

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

@e1ferapontov
Copy link

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Return value: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

You should learn how splice works.

@Robiullah2244
Copy link
Author

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

Return value: An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.

You should learn how splice works.

Oh sorry. It's my fault. And many many thanks

@facebook facebook locked as resolved and limited conversation to collaborators Oct 4, 2021
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Oct 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants