added useRef, useContext examples

This commit is contained in:
szabomarton 2025-01-17 09:17:41 +01:00
parent 31d0780080
commit 9c5ca86086
26 changed files with 114 additions and 12 deletions

View File

@ -7,11 +7,12 @@
"": { "": {
"name": "mai", "name": "mai",
"version": "0.1.0", "version": "0.1.0",
"license": "ISC",
"dependencies": { "dependencies": {
"cra-template": "1.2.0", "cra-template": "1.2.0",
"react": "19.0.0", "react": "^19.0.0",
"react-dom": "19.0.0", "react-dom": "^19.0.0",
"react-scripts": "5.0.1" "react-scripts": "^5.0.1"
} }
}, },
"node_modules/@alloc/quick-lru": { "node_modules/@alloc/quick-lru": {
@ -13722,7 +13723,6 @@
"version": "5.0.1", "version": "5.0.1",
"resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz", "resolved": "https://registry.npmjs.org/react-scripts/-/react-scripts-5.0.1.tgz",
"integrity": "sha512-8VAmEm/ZAwQzJ+GOMLbBsTdDKOpuZh7RPs0UymvBR2vRk4iZWCskjbFnxqjrzoIvlNNRZ3QJFx6/qDSi6zSnaQ==", "integrity": "sha512-8VAmEm/ZAwQzJ+GOMLbBsTdDKOpuZh7RPs0UymvBR2vRk4iZWCskjbFnxqjrzoIvlNNRZ3QJFx6/qDSi6zSnaQ==",
"license": "MIT",
"dependencies": { "dependencies": {
"@babel/core": "^7.16.0", "@babel/core": "^7.16.0",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.3", "@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
@ -16116,17 +16116,16 @@
} }
}, },
"node_modules/typescript": { "node_modules/typescript": {
"version": "5.7.2", "version": "4.9.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.2.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz",
"integrity": "sha512-i5t66RHxDvVN40HfDd1PsEThGNnlMCMT3jMUuoh9/0TaqWevNontacunWyN02LA9/fIbEWlcHZcgTKb9QoaLfg==", "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==",
"license": "Apache-2.0",
"peer": true, "peer": true,
"bin": { "bin": {
"tsc": "bin/tsc", "tsc": "bin/tsc",
"tsserver": "bin/tsserver" "tsserver": "bin/tsserver"
}, },
"engines": { "engines": {
"node": ">=14.17" "node": ">=4.2.0"
} }
}, },
"node_modules/unbox-primitive": { "node_modules/unbox-primitive": {

View File

@ -6,7 +6,7 @@
"cra-template": "1.2.0", "cra-template": "1.2.0",
"react": "^19.0.0", "react": "^19.0.0",
"react-dom": "^19.0.0", "react-dom": "^19.0.0",
"react-scripts": "5.0.1" "react-scripts": "^5.0.1"
}, },
"scripts": { "scripts": {
"start": "react-scripts start", "start": "react-scripts start",
@ -31,5 +31,9 @@
"last 1 firefox version", "last 1 firefox version",
"last 1 safari version" "last 1 safari version"
] ]
} },
"description": "This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).",
"main": "index.js",
"author": "",
"license": "ISC"
} }

View File

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.2 KiB

After

Width:  |  Height:  |  Size: 5.2 KiB

View File

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

@ -3,11 +3,15 @@ import './App.css';
import Auto from './Auto'; import Auto from './Auto';
import Szamlalo from './Szamlalo'; import Szamlalo from './Szamlalo';
import Adatlekero from './Adatlekero'; import Adatlekero from './Adatlekero';
import Keslelteto from './Keslelteto';
import Idozito from './Idozito';
import ContextPelda from './ContextPelda';
import UseRefPelda from './UseRefPelda';
function App() { function App() {
return ( return (
<> <>
<Szamlalo></Szamlalo> <UseRefPelda/>
</> </>
); );
} }

View File

@ -0,0 +1,31 @@
import { useEffect, useState, createContext, useContext } from "react";
let UserContext = createContext();
function ContextPelda(){
let [name, setName] = useState("Pop Simon");
return (
<UserContext.Provider value={name}>
<h1>Üdv, {name}</h1>
<Content2></Content2>
</UserContext.Provider>
)
}
function Content2(){
return (
<Component3></Component3>
)
}
function Component3(){
let name = useContext(UserContext);
return (
<>
Üdv újra, {name}
</>
)
}
export default ContextPelda;

View File

@ -0,0 +1,20 @@
import {useState, useEffect} from "react";
function Idozito(){
let [counter, setCounter] = useState(0);
useEffect(() => {
let timer =
setInterval(() => {
setCounter((counter) => counter + 1);
}, 1000);
return () => {
clearInterval(timer);
}
}, []);
return <h1>A counter értéke: {counter}</h1>;
}
export default Idozito;

View File

@ -0,0 +1,20 @@
import {useState, useEffect} from "react";
function Keslelteto(){
let [counter, setCounter] = useState(0);
useEffect(() => {
let timer =
setTimeout(() => {
setCounter((counter) => counter + 1);
}, 3000);
return () => {
clearTimeout(timer);
}
}, []);
return <h1>A counter értéke: {counter}</h1>;
}
export default Keslelteto;

View File

@ -0,0 +1,24 @@
import {useState, useEffect, useRef} from 'react';
function UseRefPelda(){
let [inputValue, setinputValue] = useState("");
let count = useRef(0);
useEffect(() => {
count.current = count.current + 1;
});
return (
<>
<input
type='text'
value={inputValue}
onChange={(event) => setinputValue(event.target.value)}
/>
<h1>Rendelések száma: {count.current}</h1>
</>
);
}
export default UseRefPelda;

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB