Home:ALL Converter>Persist state in localStorage and retrieving state in componentDidMount

Persist state in localStorage and retrieving state in componentDidMount

Ask Time:2019-03-11T10:09:26         Author:Riya

Json Formatter

I am trying to persist the state of Button option value in localStorage when the user selects it and retrieve the state in componentDidMount method but I am getting a null value. When the user navigates back to the page then state value should be there. Can anyone tell me what's wrong with my code?

code::

import React, { Component } from "react";
import { Button } from "semantic-ui-react";
import { withRouter } from "react-router";
import Answers from "../Answers/Answers";

class Section extends Component {
    state = {
        que1: "",
        que2: "",
        que3: ""
    };

    handleClick = event => {
        this.setState(
            {
                que1: event.target.attributes.getNamedItem("data-key").value
            }

        ),
            () => {
                localStorage.setItem("que1", que1);
                console.log(this.state.que1);
            }
    };

    handleClick2 = event => {
        this.setState(
            {
                que2: event.target.attributes.getNamedItem("data-key").value
            }
        ),
            () => {
                localStorage.setItem("que2", que2);
                console.log(this.state.que2);
            }
    };

    handleClick3 = event => {
        this.setState(
            {
                que3: event.target.attributes.getNamedItem("data-key").value
            }
        ),
            () => {
                localStorage.setItem("que3", que3);
                console.log(this.state.que3);
            }
    };

    componentDidMount() {
        this.setState({
            que1: localStorage.getItem("que1"),
            que2: localStorage.getItem("que2"),
            que3: localStorage.getItem("que3")
        });
    }

    render() {
        console.log(this.state);

        let styles = {
            width: '50%',
            margin: '0 auto',
            marginBottom: '15px'
        }
        const { history } = this.props;
        const { que1, que2, que3 } = this.state;
        return (
            <>
                <p>1. I was stressed with my nerves on edge.</p>
                <Button.Group widths="5" onClick={this.handleClick} style={styles}>
                    <Answers selected={this.state.que1} style={{ backgroundColor: 'red' }} />
                </Button.Group>
                {` `}
                <p>2. I lost hope and wanted to give up when something went wrong.</p>
                <Button.Group widths="5" onClick={this.handleClick2} style={styles}>
                    <Answers selected={this.state.que2} style={{ backgroundColor: 'red' }} />
                </Button.Group>
                {` `}
                <p>3. I feel very satisfied with the way I look and act</p>
                <Button.Group widths="5" onClick={this.handleClick3} style={styles}>
                    <Answers selected={this.state.que3} style={{ backgroundColor: 'red' }} />
                </Button.Group>
                <p />
                {` `}
                <Button
                    disabled={!que1 || !que2 || !que3}
                    onClick={() => history.push("/section2", [this.state])}
                >
                    NEXT
        </Button>
            </>
        );
    }
}

export default withRouter(Section);

output ::

enter image description here

Author:Riya,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/55094388/persist-state-in-localstorage-and-retrieving-state-in-componentdidmount
sbso :

In your onclick handlers, you have:\n\n handleClick2 = event => {\n this.setState(\n {\n que2: event.target.attributes.getNamedItem(\"data-key\").value\n }\n ),\n () => {\n localStorage.setItem(\"que2\", que2);\n console.log(this.state.que2);\n }\n };\n\n\nIn the setState callback, you need to change it to(you logged the correct value, but didn't store it properly):\n\nlocalStorage.setItem(\"que2\", this.state.que2);\n",
2019-03-11T02:23:37
yy