【问题标题】:React Native Wheel Color Picker - naeemur - Not sure how to actually access the colorReact Native Wheel Color Picker - naeemur - 不确定如何实际访问颜色
【发布时间】:2021-09-14 11:51:26
【问题描述】:

我正在尝试获取颜色选择器的结果,以便我可以在单独的组件中不断更新卡片的颜色。颜色选择器用于卡片的颜色!我尝试了一些事情,但大多数都以未定义或错误的形式返回。问题是要放入setCardColour() 我有this.currentColor 但这不起作用。有什么想法吗?

  • 这是颜色变化时的功能:

    function changeCardColour() {
      setCardColour(this.currentColour)
      console.log(cardColour)
    }
    
  • 这是颜色选择器本身:

    <ColourPicker
      color = {cardColour}
      swatches = {false}
      style = {styles.colourWheel}
      onColorChange = {changeCardColour}
    />
    
  • I would like the card to change colour

【问题讨论】:

    标签: javascript react-native colors


    【解决方案1】:

    您需要更改代码,例如,

    function changeCardColour(selectedColour) {
      setCardColour(selectedColour)
    }
    

    根据文档, onColorChange: (color) =&gt; {} 滑块和滚轮拇指移动的回调函数。

    const RoundedColorPicker = () => {
      const [currentColor, setCurrentColor] = useState('#000');
    
      const onColorChange = (selectedColor: string) => {
        setCurrentColor(selectedColor);
      };
    
      return (
        <SafeAreaView style={RoundedColorPickerStyle.container}>
          <View
            style={[
              RoundedColorPickerStyle.currentColorContainer,
              {backgroundColor: currentColor},
            ]}
          />
          <Text>Current Color:{currentColor}</Text>
          <ColorPicker
            color={currentColor}
            swatchesOnly={false}
            onColorChange={onColorChange}
            thumbSize={40}
            sliderSize={40}
            noSnap={true}
            row={false}
          />
        </SafeAreaView>
      );
    };
    

    我就是这样用的,对我来说效果很好。

    【讨论】:

    • 感谢您回复我!它似乎不喜欢被传递参数。它无法识别回调函数括号中的“color”或“this.color”。
    • 我们是否有机会创建一个色轮实际改变某物颜色的示例?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-20
    • 2022-12-07
    相关资源
    最近更新 更多