Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

dev: show Malik how to save informatin with local storage #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/components/Applause.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@ import '../App.css';

class Applause extends React.Component {
constructor() {
super ();
this.state = {
numOfClaps: 0
super()

let numOfClaps = 0 //Valor default
// O component acabou de inicializar. Vamos buscar a informacao e por numa variavel
let savedData = localStorage.getItem("numOfClaps")
// A saved data é uma string (local storage devolve sempre strings). Mas precisamos de converter para um numero
// Se savedData for null entao o numero default (0) ja esta bom. Nao precisamos de fazer nada
// Mas caso seja diferente de null como verificamos neste if. entao significa que tem la qualquer coisa que precisamos
if (savedData !== null) {
let numOfClaps = parseInt(savedData)
}

this.state = { // Isto é shorthand syntax para "numOfClaps: numOfClaps", tanto faz.
numOfClaps
}
}

makeItClap () {
makeItClap () { // Aqui queremos nao so mostrar a alteracao como tambem gravar a mesma
localStorage.setItem("numOfClaps", this.state.numOfClaps + 1)
this.setState({
numOfClaps: this.state.numOfClaps + 1
})
Expand All @@ -27,4 +39,4 @@ class Applause extends React.Component {
}
}

export default Applause;
export default Applause;