Skip to content

Instantly share code, notes, and snippets.

View haldarmahesh's full-sized avatar

Mahesh Haldar haldarmahesh

View GitHub Profile
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>
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);
}
render() {
return (
<div>
<Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/> <Brick/>
<Brick/> <Brick/> <Brick/> <Brick/>
</div>
)
}
componentWillUnmount() {
this.chart.destroy();
this.resetLocalStorage();
this.clearSession();
}
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?
shouldComponentUpdate(nextProps, nextState) {
let shouldUpdate = this.props.status !== nextProps.status;
return shouldUpdate;
}
componentDidMount() {
if (this.props.modules) {
this.props.modules.forEach(function (module) {
module(Highcharts);
});
}
// Set container which the chart should render to.
this.chart = new Highcharts[this.props.type || "Chart"](
this.props.container,
this.props.options
render() {
<div>
<PlayHeader>
<Status/>
<VolumeBar/>
<SeekBar/>
</PlayHeader>
</div>
}
@haldarmahesh
haldarmahesh / getDerivedStateFromProps-react.js
Created October 19, 2018 06:34
react lifecycle 16.4 blog
static getDerivedStateFromProps(props, state) {
if (state.value !== props.value) {
return {
derivedValue: deriveValueFromProps(props),
mirroredProp: props.value
}
}
// when null is returned no update is made to the state
return null;
}
@haldarmahesh
haldarmahesh / react-constructor-16.4.jsx
Created October 17, 2018 07:59
react-constructor-16.4.jsx
class ContraMusicPlayer extends React.Component
constructor(props) {
super(props);
this.state = {
volume: 70,
status: 'pause'
}
}