Skip to content

Instantly share code, notes, and snippets.

View MinthangML's full-sized avatar
👽
Focusing

MinthangML

👽
Focusing
View GitHub Profile
@MinthangML
MinthangML / modifyRefInuseEffect.js
Created December 21, 2020 05:05
The right way to modify ref
import React, { useRef } from "react";
const RenderCounter = () => {
const counter = useRef(0);
useEffect(() => {
// component ကို re-render လုပ်ပြီးတဲ့အချိန်တိုင်း counter က increase လုပ်နေပါလိမ့်မည်။
counter.current = counter.current + 1;
});
@MinthangML
MinthangML / modifyRefInRenderPhase.js
Created December 21, 2020 05:02
Modify Ref in Render Phase
import React, { useRef } from "react";
const RenderCounter = () => {
const counter = useRef(0);
// "Render phase" မှာ ref ရဲ့ value ကို update လုပ်မယ်ဆိုရင်
// အဲဒီ value က တစ်ကြိမ်ထက်မက increment လုပ်နိုင်ပါတယ်။
counter.current = counter.current + 1;
return (
<h1>{`The component has been re-rendered ${counter} times`}</h1>
@MinthangML
MinthangML / mutableVarInRef.js
Created December 21, 2020 04:56
Store a mutable variable in Ref
import React, { useRef, useEffect } from "react";
const Timer = () => {
const intervalRef = useRef();
useEffect(() => {
const id = setInterval(() => {
console.log("A second has passed");
}, 1000);
@MinthangML
MinthangML / useRefInFuncCompo.js
Created December 21, 2020 04:47
useRef in functional component
import React, { useRef } from "react";
const CustomTextInput = () => {
const textInput = useRef();
focusTextInput = () => textInput.current.focus();
return (
<>
<input type="text" ref={textInput} />
<button onClick={focusTextInput}>Focus the text input</button>
</>
@MinthangML
MinthangML / userefinclasscompo.js
Last active December 21, 2020 04:40
useRef in class component
import React, { Component, createRef } from "react";
class CustomTextInput extends Component {
textInput = createRef();
focusTextInput = () => this.textInput.current.focus();
render() {
return (
<>
<input type="text" ref={this.textInput} />