React Native 0.62.2应用程序中的一个组件Itie
具有一个子视图组件ButtonInHead
。当状态ButtonInHead
变为post
时,true
返回一个启用的保存按钮。这是代码:
import { Button, Left, Right, Body, Icon, Label, Item, Input, Text, Card, CardItem, Header, Container, Content, Form } from 'native-base';
const Itie = () => { //<<<===component Itie
const [post, setPost] = useState(false); //<<<==state post is false initially
const ButtonInHeader = ({enabled}) => { //<<<==sub view component
if (enabled) {
return (<Button hasText onPress={clickSave}>
<Text>Save</Text>
</Button>)
} else {
return (<Button hasText disabled >
<Text>Save</Text>
</Button>)
};
};
const clickSave = async () => {
Alert.alert("Save clicked");
};
return (
<SafeAreaView>
<ScrollView>
<Container>
<Header transparent>
<Right>
<ButtonInHeader enable={post} /> //<<<==when state post becomes true. button shall be enabled
</Right>
</Header>
<Content>
<Form>
......
</Form>
</Content>
</Container>
</ScrollView>
</SafeAreaView>
);
};
问题是post
变成true
之后,未重新加载子视图组件ButtonInHead
(在android模拟器上)并且按钮保持禁用状态。由于状态post
被传递到ButtonInHead
中,并且我的理解是,只要post
发生变化,ButtonInHead
就应该重新加载。显然,这还不是全部。如何在ButtonInHead
更改时强制重新加载post
?
答案 0 :(得分:0)
通过执行以下操作将状态post
引入ButtonInHead
中来解决问题:
return (<Button hasText onPress={clickSave} disabled={!post}>
<Text>Save</Text>
</Button>)
实际上,ButtonInHead
不再需要,可以替换为:
<Button hasText onPress={clickSave} disabled={!post}>
<Text>Save</Text>
</Button>