【问题标题】:react native : How do I make the text label that will check the checkbox while press on it?react native:如何制作文本标签,在按下复选框时会选中它?
【发布时间】:2021-06-06 04:47:57
【问题描述】:

如何制作文本“BIRD”标签以在按下复选框时选中它? 根据我的示例,它不起作用,我也不清楚。

import CheckBox from '@react-native-community/checkbox';

function App(props) {
  const isSelected = useSelector((rootState) => rootState.BitzuaDigumReducer.checkBox);

  return (
    <TouchableOpacity
      activeOpacity={1}
      style={{
        flexDirection: 'row',
        alignSelf: 'flex-start',
        top: 20,
        left: 10,
      }}
    >
      <CheckBox
        value={isSelected}
        onValueChange={(value) => dispatch(setCheckBox(value))}
        style={styles.checkbox}
        tintColors={{ true: 'white', false: 'white' }}
      />
      <Text style={styles.label}>BIRD</Text>
    </TouchableOpacity>
  );
}

【问题讨论】:

    标签: javascript reactjs react-native checkbox react-hooks


    【解决方案1】:

    我在此示例中模拟了 Checkbox,但请查看它。它应该让您了解如何通过按标签来切换复选框值:https://snack.expo.io/@zvona/toggle-checkbox-on-label

    核心代码:

    const App = () => {
      const [isSelected, setSelected] = useState(false);
    
      const toggleCheckbox = () => {
        setSelected(!isSelected);
        // Here you can dispatch the event to state management
      };
    
      return (
        <View style={styles.container}>
          <Checkbox onValueChange={toggleCheckbox} selected={isSelected} />
          <TouchableOpacity activeOpacity={0.8} onPress={toggleCheckbox}>
            <Text style={styles.label}>{'Label'}</Text>
          </TouchableOpacity>
        </View>
      );
    };
    

    【讨论】:

    • 你能说明你是如何派送它的吗?我需要我的例子会起作用
    • @Kira24 我认为您需要详细说明一下。您要发送什么(复选框值?)以及在哪里(Redux?)。你是想在每个动作上发送它还是只在(假设的)表单数据发布到某个地方时发送它
    • 如果您单击复选框本身,我需要 BIRD 标签来完全标记复选框,它会激活调度,就像您单击复选框本身时一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 2021-06-05
    • 2014-02-26
    • 1970-01-01
    • 2015-06-21
    相关资源
    最近更新 更多