how to set session storage in javascript


// Save data to sessionStorage
sessionStorage.setItem('key', 'value');

// Get saved data from sessionStorage
let data = sessionStorage.getItem('key');

// Remove saved data from sessionStorage
sessionStorage.removeItem('key');

// Remove all saved data from sessionStorage
sessionStorage.clear();

// Store
sessionStorage.setItem("lastname", "Smith");
// Retrieve

document.getElementById("result").innerHTML = sessionStorage.getItem("lastname");

localStorage.setItem("mytime", Date.now());

var setsession = window.sessionStorage.setItem("animals", "cat");
var getsession = window.sessionStorage.getItem("animals");
console.log(getsession);

www.codegrepper.com
www.stackoverflow.com

how to set state when change viewport react


import React, { useLayoutEffect, useState } from 'react';

function useWindowSize() {
const [size, setSize] = useState([0, 0]);
useLayoutEffect(() => {
function updateSize() {
setSize([window.innerWidth, window.innerHeight]);
}
window.addEventListener('resize', updateSize);
updateSize();
return () => window.removeEventListener('resize', updateSize);
}, []);
return size;
}

function ShowWindowDimensions(props) {
const [width, height] = useWindowSize();
return <span>Window size: {width} x {height}</span>;
}

www.codegrepper.com
www.stackoverflow.com

how to set three js canvas width 100%


body { margin: 0; }
canvas { width: 100vw; height: 100vh; display: block; }

"use strict";

const renderer = new THREE.WebGLRenderer();
document.body.appendChild(renderer.domElement);

// There's no reason to set the aspect here because we're going
// to set it every frame anyway so we'll set it to 2 since 2
// is the the aspect for the canvas default size (300w/150h = 2)
const camera = new THREE.PerspectiveCamera(70, 2, 1, 1000);
camera.position.z = 400;

const scene = new THREE.Scene();
const geometry = new THREE.BoxGeometry(200, 200, 200);
const material = new THREE.MeshPhongMaterial({
color: 0x555555,
specular: 0xffffff,
shininess: 50,
shading: THREE.SmoothShading
});

const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);

const light1 = new THREE.PointLight(0xff80C0, 2, 0);
light1.position.set(200, 100, 300);
scene.add(light1);

function resizeCanvasToDisplaySize() {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
if (canvas.width !== width ||canvas.height !== height) {
// you must pass false here or three.js sadly fights the browser
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();

// set render target sizes here
}
}

function animate(time) {
time *= 0.001; // seconds

resizeCanvasToDisplaySize();

mesh.rotation.x = time * 0.5;
mesh.rotation.y = time * 1;

renderer.render(scene, camera);
requestAnimationFrame(animate);
}

requestAnimationFrame(animate);

www.codegrepper.com
www.stackoverflow.com

how to set validation for email in javascript


<script>
function validateform(){
var name=document.myform.name.value;
var password=document.myform.password.value;

if (name==null || name==""){
alert("Name can't be blank");
return false;
}else if(password.length<6){
alert("Password must be at least 6 characters long.");
return false;
}
}

/* JavaScript: validating email address */

isValidEmail("xyz@abc.com"); // true

function isValidEmail(email) {
var emailRegex = /^w+([.-]?w+)*@w+([.-]?w+)*(.w{2,3})+$/;
return !!email && typeof email === 'string'
&& email.match(emailformat)};
};

<script language="javascript">

function checkEmail() {

var email = document.getElementById('txtEmail');
var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;

if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}</script>

www.codegrepper.com
www.stackoverflow.com

how to set value in array react hook usestate


const[array,setArray]= useState([
{id: 1, value: "a string", othervalue: ""},
{id: 2, value: "another string", othervalue: ""},
{id: 3, value: "a string", othervalue: ""},
])

const updateItem =(id, whichvalue, newvalue)=> {
var index = array.findIndex(x=> x.id === id);

let g = array[index]
g[whichvalue] = newvalue
if (index === -1){
// handle error
console.log('no match')
}
else
setArray([
...array.slice(0,index),
g,
...array.slice(index+1)
]
);
}
//how to use the function
onPress={()=>updateItem(2,'value','ewfwf')}
onPress={()=>updateItem(1,'othervalue','ewfwf')}
/*
the first input of the function is the id of the item
the second input of the function is the atrribute that you wish to change
the third input of the function is the new value for that attribute
It's a pleasure :0
~atlas
*/

setTheArray([...theArray, newElement]);

const {useState, useCallback} = React;
function Example() {
const [theArray, setTheArray] = useState([]);
const addEntryClick = () => {
setTheArray([...theArray, `Entry ${theArray.length}`]);
};
return [
<input type="button" onClick={addEntryClick} value="Add" />,
<div>{theArray.map(entry =>
<div>{entry}</div>
)}
</div>
];
}

ReactDOM.render(
<Example />,
document.getElementById("root")
);

www.codegrepper.com
www.stackoverflow.com

how to set background automatically with my screen height


html {
background: url(image.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

body {

  background-image: url("img_tree.gif");

  background-repeat: no-repeat;  
background-attachment: fixed;

background-position: top center;
background-size: contain;

}

www.codegrepper.com
www.stackoverflow.com