Validation using formik

I am using Formik , i have a MapScreen where i am getting the location and setting it to the state and I am using AppFormLocation component to validate the field , but when i select the the location and it gets displayed on the text field and the error gets removed but when i try to sumbit the form the error is still there behind the scene its preventing me to submit the form

```JavaScript

const AppFormLocation = ({ name, value, setError, ...otherProps }) => {
  const {
    setFieldTouched,
    errors,
    touched,
    setFieldValue,
    handleChange,
    values,
  } = useFormikContext();

  const shouldShowError = touched[name] && !value;
  console.log("shop locations=>", errors[name]);
  const handleBlur = () => {
    setFieldTouched(name);
    if (!value) {
      setError(true);
    } else {
      setError(false);
    }
  };

  return (
    <>
      <AppTextInput
        value={value}
        onBlur={handleBlur}
        onChangeText={handleChange(name)}
        {...otherProps}
      />
      {shouldShowError && (
        <ErrorMessage error={errors[name]} visible={shouldShowError} />
      )}
    </>
  );
};
///Parent Component
const validationSchema = Yup.object().shape({
  cnic: Yup.string()
    .matches(/^d{13}$/, "Invalid NIC Number")
    .required("NIC Number is required"),
  shopCategory: Yup.object().nullable().required().label("Shop Category"),
  shopName: Yup.string().required().label("Shop Name").max(20),
  shopLocation: Yup.string()
    .required("Shop Location is Required")
    .label("Shop Location"),
});

const OwnerCompleteProfile = ({ navigation, route }) => {
  const [userData, setUserData] = useState(route.params.info);
  const [selectedItem, setIsSelectedItem] = useState();
  const [selectedLocation, setSelectedLocation] = useState(null);
  const [checked, setChecked] = useState(false);
  const toggleCheckbox = () => setChecked(!checked);
  const [error, setError] = useState(false); // State to manage error
  const handleSelectItem = (item) => {
    setIsSelectedItem(item);
  };

  const { location } = route.params || {};

  useEffect(() => {
    if (location) {
      // console.log("location: =>", location);
      const concatenatedLocation = `${location?.reverseGeocodeName || ""} ${
        location?.description || ""
      }`;
      handleChangeLocation(concatenatedLocation.trim());
    }
  }, [location]);

  const handleChangeLocation = (locationData) => {
    setSelectedLocation(locationData);
    if (!locationData) {
      setError(true);
    } else {
      setError(false);
    }
  };

  console.log("error:", error);
  const handleMapView = () => {
    navigation.navigate("MapScreen", {
      onLocationSelect: selectedLocation,
    });
  };

  return (
    <KeyboardAvoidingView
      behavior={Platform.OS === "ios" ? "padding" : "height"}
      keyboardVerticalOffset={Platform.OS === "ios" ? -64 : -100}
      style={{ flex: 1 }}
    >
      <AppForm
        initialValues={{
          ...userData,
          // image: "",
          cnic: "",
          shopCategory: "",
          shopName: "",
          shopLocation: selectedLocation,
          latitude: location?.longitude,
          longitude: location?.latitude,
        }}
        onSubmit={(values) =>
          navigation.navigate("ShopTimeScreen", {
            info: values,
          })
        }
        validationSchema={validationSchema}
      >
        <View style={styles.container}>
          <AppChevronLeft style={styles.backButton} />
          <AppText style={styles.text}>Complete Your Profile</AppText>
          <View style={styles.imageContainer}>
            <ImageInput />
          </View>
          <View
            style={{
              bottom: "8%",
              // backgroundColor: "yellow",
              alignItems: "center",
            }}
          >
            <AppFormField
              name="cnic"
              autoCorrect={false}
              maxLength={40}
              placeholder="CNIC"
              keyboardType="default"
            />
            <AppFormPicker
              // selectedItem={selectedItem}
              // onSelectItem={handleSelectItem}
              name="shopCategory"
              items={categories}
              placeholder="Shop Categories"
            />
            <AppFormField
              name="shopName"
              autoCorrect={false}
              maxLength={40}
              placeholder="Shop Name"
              keyboardType="default"
            />
            <View style={{ width: "100%" }}>
              <AppFormLocation
                value={selectedLocation}
                onChangeText={setSelectedLocation}
                autoCorrect={false}
                maxLength={40}
                placeholder="Shop Location"
              />
              <TouchableOpacity
                onPress={handleMapView}
                style={styles.iconContainer}
              >
                <FontAwesome5
                  name="map-marker-alt"
                  size={normalize(17)}
                  color="black"
                />
              </TouchableOpacity>
            </View>
          </View>
          <SubmitButton
            btnIconName="arrow-right"
            title="Next"
            style={styles.button}
          />
        </View>
      </AppForm>
    </KeyboardAvoidingView>
  );
};

New contributor

Taha Husnain is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật