【发布时间】:2020-01-05 13:26:34
【问题描述】:
我正在将我的大组件调制成小块。在父组件中,我有很多 Switch 和 CheckbBox 组件。
在这里
ParentComponent.js
<View style={styles.sliderContainer}>
<Text style={styles.sliderLabel}>Diettags:</Text>
<CheckBoxComponent label={'Sugarfree'} availableItems={13}/>
<CheckBoxComponent label={'Youth'} availableItems={11}/>
<CheckBoxComponent label={'Metabolites'} availableItems={10}/>
<CheckBoxComponent label={'Fodmap'} availableItems={7}/>
</View>
CheckBoxCompoenent.js
import React, {Component} from 'react'
import {Text, View, CheckBox, Switch, Platform,StyleSheet} from "react-native";
import PropTypes from 'prop-types'
class CheckBoxComponent extends Component {
state={
checked: true
};
render() {
const { label,handleToggle,availableItems} = this.props;
const {checked} = this.state;
return (
Platform.OS === 'ios' ?
<View style={styles.mainContainer}>
<Text style={styles.label}>{`${label}(${availableItems})`}</Text>
<Switch value={checked}/>
</View> :
<View style={styles.mainContainer}>
<Text style={styles.label}>{`${label}(${availableItems})`}</Text>
<CheckBox value={checked} />
</View>
)
}
}
const styles = StyleSheet.create({
mainContainer: {
flex: 1,
justifyContent: 'space-between',
alignItems: 'center',
flexDirection: 'row',
marginLeft: 20,
marginRight: 20,
marginTop: 10
},
label: {
fontSize: 16,
}
});
CheckBoxComponent.proptype = {
label: PropTypes.string.isRequired,
handleToggle: PropTypes.func.isRequired,
availableItems: PropTypes.string
};
export default CheckBoxComponent
现在我想要什么
我想在我的父组件中处理开关和复选框的onValueChange
我已经尝试了 stackoverflow 上所有可用的方法,但没有任何帮助。
请有人指导我通过代码如何解决这个问题。 ?
非常感谢
【问题讨论】:
-
您可以使用 react-native-elements 为两个操作系统使用复选框
-
如果你不制作钩子!然后它的简单使用传递函数作为道具!到子组件来处理状态变化!这里我举个例子
标签: react-native components state reusability modularity