0

I am trying to update the useState() array by passing through value of TextInput (onChangeText). The code is working fine however whenever I am clinking onPress (addBook function) it is updating/ adding previous text in the array not current one. So, to add current text I have to click button twice which is creating dupliate items in my array.

I may be making very studpid misatke but cant find it. Can any one help. Below is my entire code.

import React, { useState } from "react";
import { TextInput, View, Dimensions, TouchableOpacity } from "react-native";
import { MaterialCommunityIcons } from "@expo/vector-icons";
import { Entypo } from "@expo/vector-icons";
const screenHeight = Math.round(Dimensions.get("window").height);
const AddBookName = ({ hidePressed }) => {
 const [book, setBook] = useState();
 const [bookList, setBookList] = useState(["no name"]);
 const addBook = () => {
 setBookList([...bookList, book]);
 console.log(bookList);
 };
 return (
 <View style={{ flexDirection: "row" }}>
 <View style={{ flex: 1 }}>
 <TextInput
 style={{
 fontSize: 20,
 paddingVertical: 10,
 backgroundColor: "#ececec",
 paddingLeft: 10,
 }}
 placeholder="Enter the Book Name"
 placeholderTextColor="grey"
 // value={book}
 onChangeText={(text) => setBook(text)}
 type="text"
 ></TextInput>
 </View>
 <TouchableOpacity
 style={{
 flex: 0.15,
 backgroundColor: "green",
 alignItems: "center",
 justifyContent: "center",
 }}
 onPress={() => addBook()} 
 >
 <View>
 <MaterialCommunityIcons name="check" size={24} color="white" />
 </View>
 </TouchableOpacity>
 <TouchableOpacity
 style={{
 flex: 0.15,
 backgroundColor: "red",
 alignItems: "center",
 justifyContent: "center",
 }}
 onPress={hidePressed}
 >
 <View>
 <Entypo name="cross" size={24} color="white" />
 </View>
 </TouchableOpacity>
 </View>
 );
};
export default AddBookName;

enter image description here

soloko
3042 silver badges15 bronze badges
asked Dec 3, 2020 at 15:03
3
  • 1
    your app is working fine, I don't see any duplication of data. Try rendering it in FlatList, everything working perfectly Commented Dec 3, 2020 at 15:18
  • I see no duplication of data as well Commented Dec 3, 2020 at 15:42
  • Hi @KetanRamteke Thanks for comment. You are correct when I am rendering the list in Flatlist its working fine. Thanks again. Commented Dec 4, 2020 at 3:00

2 Answers 2

0

Try to clean the previous input value after adding to the list.

 const addBook = () => {
 if (book) {
 setBookList([...bookList, book]);
 console.log(bookList);
 setBook("");
 }
 };
answered Dec 3, 2020 at 16:02
Sign up to request clarification or add additional context in comments.

Comments

0

This should remove duplicates:

const addBook = () => {
 setBookList(Array.from(new Set([...bookList, book])));
 console.log(bookList);
};

You could also store your booklist as a set instead of an array.

answered Dec 3, 2020 at 18:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.