Javascript 如何更新其他组件中的状态
嘿,我还是react natve的新手,在我的react原生项目中有一个自定义的复选框包装器Javascript 如何更新其他组件中的状态,javascript,reactjs,react-native,checkbox,Javascript,Reactjs,React Native,Checkbox,嘿,我还是react natve的新手,在我的react原生项目中有一个自定义的复选框包装器 类CheckBoxWrapper扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ 勾选:假, 地点:“ } } render(){ 返回( { this.setState({checked:!this.state.checked}); this.props.onCheck(this.props.value); }} /> ); } }试试这个 class Ch
类CheckBoxWrapper扩展React.Component{
建造师(道具){
超级(道具);
此.state={
勾选:假,
地点:“
}
}
render(){
返回(
{
this.setState({checked:!this.state.checked});
this.props.onCheck(this.props.value);
}}
/>
);
}
}
试试这个
class CheckBoxWrapper extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: false, //remove it
location: ""
}
}
render() {
return (
<View style={this.props.style} >
<CheckBox
checkedIcon={this.props.checkedIcon}
uncheckedIcon={this.props.uncheckedIcon}
checkedColor={this.props.checkedColor}
uncheckedColor={this.props.uncheckedColor}
textStyle={this.props.textStyle}
containerStyle={{ flex: 0.6, margin: 0, borderWidth: 0 }}
size={this.props.size ? this.props.size : normalize(18)}
title={this.props.title}
checked={this.props.checked} // just add props here and
onPress={() => {
this.setState({ checked: !this.props.checked }); // here
this.props.onCheck(this.props.value);
}}
/>
</View>
);
}
}
类CheckBoxWrapper扩展React.Component{
建造师(道具){
超级(道具);
此.state={
选中:false,//将其删除
地点:“
}
}
render(){
返回(
{
this.setState({checked:!this.props.checked});//此处
this.props.onCheck(this.props.value);
}}
/>
);
}
}
不仅仅是这样简单吗
import React, { useState } from 'react'
const yourComponent = () => {
const [someValue, setSomeValue] = useState('');
}
...
<Item fixedLabel style={{ height: 40 }}>
<Label>Push Notifications</Label>
<Switch
value={pushNotifications}
selectedValue={pushNotifications}
onValueChange={value => {
setPushNotifications(value)
}}
/>
</Item>
import React,{useState}来自“React”
constyourcomponent=()=>{
const[someValue,setSomeValue]=useState(“”);
}
...
推送通知
{
setPushNotifications(值)
}}
/>
React中有很多关于从一个组件更新另一个组件状态的问题,快速的谷歌搜索应该会显示出很多问题。简言之,您要么将状态/功能提升到一个共同的祖先,要么使用像Redux这样的集中存储。在您的情况下,将状态/功能提升到页面组件就足够了,我无法选中或取消选中复选框,状态为UnfinedOkay,请将this.props.checked添加到状态。等等,我会编辑。这就是我能分享的,你想知道哪一部分?第二个截图中没有完整的代码。我发现那个区域可能有问题。您在这两个组件中都创建了选中状态。我将编辑我的代码。希望它能帮助您