Skip to content

Instantly share code, notes, and snippets.

View haldarmahesh's full-sized avatar

Mahesh Haldar haldarmahesh

View GitHub Profile
shouldComponentUpdate(nextProps, nextState) {
let shouldUpdate = this.props.status !== nextProps.status;
return shouldUpdate;
}
type Snapshot = number | null;
class ScrollingList extends React.Component<Props, State, Snapshot> {
listRef = React.createRef();
getSnapshotBeforeUpdate(
prevProps: Props,
prevState: State
): Snapshot {
// Are we adding new items to the list?
componentWillUnmount() {
this.chart.destroy();
this.resetLocalStorage();
this.clearSession();
}
render() {
return (
<div>
<Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/>
<Brick/> <Brick/> <Brick/> <Brick/>
</div>
)
}
import React, { Component } from 'react'
export default class Counter extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
}
this.handleCounter = this.handleCounter.bind(this);
}
import React, { useState } from 'react';
export default function counterHook() {
const [count, setCount] = useState(0);
return (
<div>
<div>
<button onClick={() => setCount(count + 1)}> + Increment</button>
<button onClick={() => setCount(count - 1)}> - Decrement</button>
</div>
<div>
const countVariables = useState(0); // return two items in an array
const count = countVariables[0]; // first item from array
const setCount = countVariables[1]; // second item from array
const [count, setCount] = useState(0);
// e.g const [a, b] = [1, 2]
// The value of a is 1
// The value of b is 2
// This is array destructuring
const [count, setCount] = useState(0);
const [todos, setTodos] = useState(['Learn Hooks', 'Implement hooks']);
const [cart, setCart] = useState({id: 'xx22', name: 'iphone 8', quantity: 1});
import React, { Component } from 'react'
export default class Counter extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
}
this.handleIncrement = this.handleCounter.bind(this);
}