【发布时间】:2020-01-13 09:08:29
【问题描述】:
我试图通过组件的回调将单选按钮实现到我的本机项目中
我在项目中使用 react-native-radio-buttons SegmentedControls
App.js
import files....
import RadioButtons from "../components/RadioButtons";
//Data
const PackingType = [{id:"1", name: "Bag"},{id: "2",name: "Box"}];
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
packTypeSelected: [],
};
}
...
renderAddPackType(selectedOption, selectedIndex) {
this.setState({ selectedIndex });
console.log(selectedIndex[0]);
}
...
render(){
return(
...
<RadioButtons
buttonOptions={PackingType}
callback={this.renderAddPackType.bind(this)}
selectedOption={"Bag"}
/>
...
)
}
RadioButtons.js
import { SegmentedControls } from "react-native-radio-buttons";
export default class RadioButtons extends Component {
onSelectedItemsChange = (selectedOption, selectedIndex) => {
this.props.callback(selectedOption, selectedIndex);
};
render() {
return (
<View style={{ marginHorizontal: 20, marginVertical: 10 }}>
<SegmentedControls
options={this.props.buttonOptions}
onSelection={(selectedOption, selectedIndex) =>
this.onSelectedItemsChange(selectedOption, selectedIndex)
}
selectedOption={this.props.selectedOption}
/>
</View>
);
}
}
错误:
Invariant Violation: Invariant Violation: Objects are not valid as a React child (found: object with keys {id, name}). If you meant to render a collection of children, use an array instead.
到目前为止,我在开发方面没有太多经验.. 请帮助解决这里的错误
感谢您的宝贵时间
【问题讨论】:
标签: javascript reactjs react-native radio-button