34
loading...
This website collects cookies to deliver better user experience
function App() {
return (
<Title head={'sample title'} text={'sample text'}/>
);
}
const Title = ({head,text}) => {
return (
<div>
<h1>{head}</h1>
<Text text={text}/>
</div>
)
}
const Text = ({text}) => {
return (
<div>
<p>{text}</p>
</div>
)
}
import { createContext } from 'react';
import Title from './components/Title';
export const SampleContext = createContext()
function App() {
const word = {
title: "'sample title',"
text: 'sample text'
}
return (
<SampleContext.Provider value={word}>
<Title />
</SampleContext.Provider>
);
}
import { useContext } from 'react'
import { SampleContext } from '../App'
import Text from './Text'
const Title = () => {
const contextTitle = useContext(SampleContext)
return (
<>
<h1>{contextTitle.title}</h1>
<Text />
</>
)
}
import { useContext } from 'react'
import { SampleContext } from '../App'
const Text = () => {
const contextText = useContext(SampleContext)
return (
<p>{contextText.text}</p>
)
}
import { useState, createContext } from 'react';
import Title from './components/Title';
export const SampleContext = createContext()
function App() {
const [title, setTitle] = useState('Default Title')
const [text, setText] = useState('Default Text')
return (
<SampleContext.Provider value={{ titles: [title, setTitle], texts: [text, setText] }}>
<Title />
</SampleContext.Provider>
);
}
import { useContext } from 'react'
import { SampleContext } from '../App'
import Text from './Text'
const Title = () => {
const {titles} = useContext(SampleContext)
const [title, setTitle] = titles
const handleClick = () => {
setTitle('Changed from child components')
}
return (
<>
<h1>{title}</h1>
<button onClick={handleClick}>Change</button>
<Text />
</>
)
}
import { useContext } from 'react'
import { SampleContext } from '../App'
const Text = () => {
const {texts}= useContext(SampleContext)
const [text, setText] = texts
const handleText = () => {
setText('Changed from grand child component.')
}
return (
<>
<p>{text}</p>
<button onClick={handleText}>change</button>
</>
)
}
const title = useContext(SampleContext.title);
import React, { useReducer } from 'react'
const Counter = () => {
const reducerFunc = (state, action) => {
switch (action) {
case 'PLUS':
return state + 1;
case 'MINUS':
return state - 1;
case 'RESET':
return 0;
default:
return state;
}
}
const [count, dispatch] = useReducer(reducerFunc, 0);
return (
<div>
<h1>{count}</h1>
<button onClick={() => dispatch('PLUS')}>+</button>
<button onClick={() => dispatch('MINUS')}>-</button>
<button onClick={() => dispatch('RESET')}>0</button>
</div>
)
}
const [state, dispatch] = useReducer(Reducer, intialState)
useMemo(() => func(that has very expensive value), deps)
useCallback(func(that has very expensive function), deps)
const refObj = useRef(initVal)
const val = refObj.current;
import React,{ useRef } from 'react'
const Input = () => {
const refObj = useRef(null)
const handleRef = () => {
refObj.current.focus()
}
return (
<div>
<input ref={refObj} type='text' />
<button onClick={handleRef}>click</button>
</div>
)
}