There is three screen thar Index.tsx, Profile.tsx, Login.tsx. I try to going Login to set AsyncStorage then back to Profile use this.state.member get from AsyncStorage to switch components inside Profile. But now I only can go back to index then profile in this way I can change sub component in Profile
I done like this before by using react-navigation createSwitchNavigator(). But now I try to re-render in Profile by using this.setState({member: (result from AsyncStorage.getItem("member") ) ? AsyncStorage.getItem("member") : undefined }) in componentDidMount()
Here's the code of the case, https://gist.github.com/sanhuang/4526dbd00051695c80b7b36dd7230ef8
export default class ProfileSreen extends Component {
constructor(props) {
super(props)
this.state = {
isLoading: true,
isSignIn: false,
member: undefined
}
this._getMemberStorage = this._getMemberStorage.bind(this);
}
componentDidMount() {
this._getMemberStorage();
}
async _getMemberStorage(): Promise<void> {
try{
// let member = null;
let member = await ExStorage.getItem("member");
// console.log(member);
if ( member !== null ){
await this.setState({
member,
isSignIn: true
});
}else{
await this.promisedSetState({
member: null,
isSignIn: false
});
}
await this.promisedSetState({
isLoading: false
});
return true;
}catch(e) {
console.error(e);
}
}
promisedSetState = (newState) => {
return new Promise((resolve) => {
this.setState(newState, () => {
resolve()
});
});
}
protected _renderLogin() {
return (
<ListItem>
<Button small rounded
onPress={() => {
this.props.navigation.navigate('Profile_Login');
}}
>
<Text>Login</Text>
</Button>
</ListItem>
)
}
protected _renderMember() {
return (
<ListItem>
<Body>
<H2>{this.state.member.cmt} {this.state.member.unit}{this.state.member.name},您好</H2>
<Text note>xxx</Text>
</Body>
</ListItem>
)
}
protected render() {
let infocoms = null;
if (this.state.isLoading ) {
return (
<L.PageLoading />
)
}else{
if (this.state.isSignIn) {
infocoms = this._renderMember();
} else {
infocoms = this._renderLogin();
}
return (
<Root>
<Container>
<Content>
<List>
{infocoms}
</List>
<Settings />
</Content>
{<L.IFooter />}
</Container>
</Root>
);
}
}
}
The result always render _renderLogin() even I logined