0

When I run my application, if I have session data in AsyncStorage, I load it, but it's being loaded after the component mounts and sent to mainContainer as undefined, I need to first load the data and then send it to MainContainer

export default class AppContainer extends Component<{}> {
    constructor(props) {
        super(props);

        this.state = {
            data: this.props.data,
            authtoken: this.props.authtoken,
            sucursal: this.props.sucursal
        };
    }


    componentWillMount() {
        if(AsyncStorage.getItem('authtoken') !== null){
        this.getDataFromStorage();
        }
    }

    async getDataFromStorage() {
        AsyncStorage.getItem('authtoken').then((token) => {
            this.setState({ authtoken: token });
            console.log(this.state.authtoken);
        });
        AsyncStorage.getItem('data').then((dataStorage) => {
            this.setState({ data: JSON.parse(dataStorage) });
            console.log(this.state.data);
        });
    }

    componentDidMount() {
        console.log(this.state.data);
        console.log(this.state.authtoken);

    }




    render(props) {
        return (
            <View style={styles.bigContainer}>
                <Header/>
                <MainContainer data={this.state.data} authtoken={this.state.authtoken} sucursal={this.state.sucursal}/>
            </View>
        );
    }

}
2

1 Answer 1

0

Introduce Loading Indicator to your component tree. Check for the value of this.state.data. Stop the indicator only once you render data.

render(props) {
    return (
        <View style={styles.bigContainer}>
            <ActivityIndicator animating={!this.state.data} size="large" color="#0000ff" />
            <Header/>
            <MainContainer data={this.state.data} authtoken={this.state.authtoken} sucursal={this.state.sucursal}/>
        </View>
    );
}

You might have to change the styling.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.