Hello I had an idea to make a hook to increase the font size and save preferences in localStorage
basically I have a state that goes from 1 to 4, and then when I click the button add I add +1 to the state until I reach number 4
and on the remove button I remove 1 from the state until 1
But I have doubts on how to save this to my location
basically if i don’t use my useState just with getInitialValue It works normally.
like this gif, If I add the value manually it works:
but if I try to use my setFont I have problems (as it is saved in localStorage):
and i got this on localStorage :
code:
export default function App() {
const { fontSize, setSize } = useFontSize();
console.log(fontSize);
return (
<div className="App">
<button
onClick={() => {
setSize(fontSize + 1);
}}
>
add
</button>
<button
onClick={() => {
setSize(fontSize + 1);
}}
>
remove
</button>
</div>
);
}
hook:
export default function useFontSize(defaultSize = { size: 1 }) {
const [fontSize, _setSize] = useState(getInitialSize);
function getInitialSize() {
const savedSize = localStorage.getItem('_size_acessibility_font');
const parsedSize = JSON.parse(savedSize);
if (parsedSize) {
const { size } = parsedSize;
if (size >= 1 && size <= 4) {
return size;
}
} else {
return defaultSize.size;
}
}
useEffect(() => {
console.log(fontSize, 'on useEffect to set on localStorage');
localStorage.setItem(
'_size_acessibility_font',
JSON.stringify({ size: fontSize }),
);
}, [fontSize]);
return {
fontSize,
setSize: ({ setSize, ...size }) => {
console.log(size, 'on function set size');
if (size > 4) {
return _setSize(4);
}
if (size < 1) {
return _setSize(1);
}
return _setSize(size);
},
};
}
example:
https://codesandbox.io/s/focused-newton-x0mqd
I don’t know if this is the best logic for this context, if someone can help me.
viagra tablets from india – price viagra 50mg buy viagra online usa no prescription
40 mg generic cialis – generic cialis cost tadalafil uk online
over the counter erectile dysfunction pills – best ed pills at gnc over the counter ed pills at cvs
cheap prednisone online – prednisone 60mg pills prednisone 4 mg daily
generic viagra for daily use – generic viagra 100mg for sale sildenafil online nz
rx pharmacy coupons – Buy branded cialis buy cialis india pharmacy
fda ivermectin – ivermectin pills human stromectol order online
949996 126170I dont typically comment but I gotta say thankyou for the post on this incredible one : D. 413414
431760 132546Really educating story, saved your web site for hopes to read much more! 990929
order viagra 50mg – buy cialis 10mg without prescription generic cialis
sildenafil 200mg pas cher – acheter viagra en ligne acheter 20mg gГ©nГ©rique tadalafil en france
prednisone 40mg us – best ed medications order prednisone 20mg online cheap
accutane 40mg for sale – buy metformin for sale order amoxil 250mg without prescription
pregabalin drug – order lasix order zithromax 500mg for sale
buy generic cialis online with mastercard tadalafil price walmart
https://cialismat.com/ tadalafil without a doctor prescription
cenforce order online – buy allopurinol 300mg sale zovirax online order
side effects of tadalafil cheap generic cialis for sale
generic tizanidine 2mg – fluoxetine over the counter gloperba canada
cialis without prescription https://cialisicp.com/
cialis without prescription cialis cost
tadalafil cost walmart buy tadalafil
where to buy tadalafil on line cost tadalafil generic
buy generic cialis online with mastercard best price usa tadalafil