Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

why it is not working to remove if it the same color name?

I have a problem

My currentColor goes not to null, If I Press a color I set the color to currentColor, if the same color is again pressed then I remove the color (I set currentColor to null). but its not working

const ModalProductSizeColor = forwardRef<BottomSheetModal, IModalProductSizeColor>(({ onPress }, ref) => {
  const navigation = useNavigation<NativeStackNavigationProp<RootStackParams>>();

  const [currentColor, setCurrentColor] = useState<ColorsOptions | null>(null);
  const [sizeScreen, setSizeScreen] = useState<boolean>(false);

  const snapPoints = useMemo(() => ['25%', 250], []);

  const handleSetCurrentColor = (color: ColorsOptions) => {
    console.log(currentColor + 'current in func');
    currentColor === color ? setCurrentColor(null)  : setCurrentColor(color)
  };

  console.log(currentColor);
  return (
    <BottomSheetModal
      ref={ref}         
      index={1}
      snapPoints={snapPoints}
      handleIndicatorStyle={[s.handleStyle, s.handleColorWhite]}
      backdropComponent={BottomSheetBackdrop}
    >
      <Text style={s.title}>test</Text>

        <Colors
          colors={ColorsData}
          onPress={handleSetCurrentColor}
          onLongPress={(x) => console.log(x)}
          containerStyle={s.containerStyle}
        />
      
      <View style={s.btnContainer}>
        <Pressable style={ButtonStyles.full}>
          <Text style={s.btnText}>Fertig</Text>
        </Pressable>
      </View>
    </BottomSheetModal>
  )
});

In my function when I console.log currentColor its null but outside of the function it is setted , I dont check anything anymore I am very thankful for your help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Assuming your Colors component passes the color to your handleSetCurrentColor, you could try to set your currentColor like this

...
console.log(currentColor + 'current in func');
setCurrentColor(prevCurrentColor => prevCurrentColor === color ? null : color)
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading