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 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 setstate in parent in child component in react native


class Parent extends React.Component {
constructor(props) {
super(props)

this.handler = this.handler.bind(this)
}

handler() {
this.setState({
someVar: 'some value'
})
}

render() {
return <Child handler = {this.handler} />
}
}

class Child extends React.Component {
render() {
return <Button onClick = {this.props.handler}/ >
}
}

www.codegrepper.com
www.stackoverflow.com

How to send form data from react to express


const url = localhost:4000/api/users/register;
const usersData= [];

let getData = () => {
axios.get(url)
.then(res => usersData.push(res.data))
.catch(err => console.log(err.data))
}

const reactData = [{ id: 1, name:' Tom'}, { id: 2, name:' Sarah'}];
const url = localhost:4000/api/users/register;

let sendData = () => {
axios.post(url, reactData)
.then(res => console.log('Data send'))
.catch(err => console.log(err.data))
}

www.codegrepper.com
www.stackoverflow.com

how to run react native app


npm install -g react-native-cli

//cd to the file where your react native projects will be
react-native init albums

// Run instructions for IOS/Android
cd projects/albums

npx react-native run-ios
npx react-native run-android

react-native run-android
react-native run-ios

npm install -g react-native-cli

npx react-native init AwesomeProject

react-native start

$ npx react-native init firstapp

www.codegrepper.com
www.stackoverflow.com

how to scroll to an element javascript react


class ReadyToScroll extends Component {

constructor(props) {
super(props)
this.myRef = React.createRef()
}

render() {
return <div ref={this.myRef}></div>
}

scrollToMyRef = () => window.scrollTo(0, this.myRef.current.offsetTop)
// run this method to execute scrolling.

}

import React, { useRef } from 'react'

const scrollToRef = (ref) => window.scrollTo(0, ref.current.offsetTop)
// General scroll to element function

const ScrollDemo = () => {

const myRef = useRef(null)
const executeScroll = () => scrollToRef(myRef)

return (
<>
<div ref={myRef}>I wanna be seen</div>
<button onClick={executeScroll}> Click to scroll </button>
</>
)
}

import { scroller } from "react-scroll";

// excluded React component syntax...

scrollToSection = () => {
scroller.scrollTo("your_css_class_here", {
duration: 800,
delay: 0,
smooth: "easeInOutQuart",
});
};

www.codegrepper.com
www.stackoverflow.com

how to see if a web site is useing react


if(!!window.React ||
!!document.querySelector('[data-reactroot], [data-reactid]'))
console.log('React.js');

if(!!window.angular ||
!!document.querySelector('.ng-binding, [ng-app], [data-ng-app], [ng-controller], [data-ng-controller], [ng-repeat], [data-ng-repeat]') ||
!!document.querySelector('script[src*="angular.js"], script[src*="angular.min.js"]'))
console.log('Angular.js');

if(!!window.Backbone) console.log('Backbone.js');
if(!!window.Ember) console.log('Ember.js');
if(!!window.Vue) console.log('Vue.js');
if(!!window.Meteor) console.log('Meteor.js');
if(!!window.Zepto) console.log('Zepto.js');
if(!!window.jQuery) console.log('jQuery.js');

www.codegrepper.com
www.stackoverflow.com