Compare commits

..

2 Commits

9 changed files with 107 additions and 162 deletions
+3 -44
View File
@@ -8,10 +8,8 @@
"name": "store-client",
"version": "0.0.0",
"dependencies": {
"immer": "^11.1.8",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"zustand": "^5.0.14"
"react-dom": "^19.1.0"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
@@ -1369,7 +1367,7 @@
"version": "19.1.6",
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.1.6.tgz",
"integrity": "sha512-JeG0rEWak0N6Itr6QUx+X60uQmN+5t3j9r/OVDtWzFXKaj6kD1BwJzOksD0FF6iWxZlbE1kB0q9vtnU2ekqa1Q==",
"devOptional": true,
"dev": true,
"license": "MIT",
"dependencies": {
"csstype": "^3.0.2"
@@ -1621,7 +1619,7 @@
"version": "3.1.3",
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==",
"devOptional": true,
"dev": true,
"license": "MIT"
},
"node_modules/debug": {
@@ -2056,16 +2054,6 @@
"node": ">= 4"
}
},
"node_modules/immer": {
"version": "11.1.8",
"resolved": "https://registry.npmjs.org/immer/-/immer-11.1.8.tgz",
"integrity": "sha512-/tbkHMW7y10Lx6i1crLjD4/OhNkRG+Fo7byZHtah0547nIeXYcpIXaUh0IAQY6gO5459qpGGYapcEOHtFXkIuA==",
"license": "MIT",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/immer"
}
},
"node_modules/import-fresh": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
@@ -2806,35 +2794,6 @@
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/zustand": {
"version": "5.0.14",
"resolved": "https://registry.npmjs.org/zustand/-/zustand-5.0.14.tgz",
"integrity": "sha512-/8tAspM5LMPr28b3fwLYrtdj77ECpfZviaP75CMTnwO8ISyaE4GDIG/9rDDYq/cH9D2Xw2A2RXglLInmVBQB/g==",
"license": "MIT",
"engines": {
"node": ">=12.20.0"
},
"peerDependencies": {
"@types/react": ">=18.0.0",
"immer": ">=9.0.6",
"react": ">=18.0.0",
"use-sync-external-store": ">=1.2.0"
},
"peerDependenciesMeta": {
"@types/react": {
"optional": true
},
"immer": {
"optional": true
},
"react": {
"optional": true
},
"use-sync-external-store": {
"optional": true
}
}
}
}
}
+2 -4
View File
@@ -4,16 +4,14 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"dev": "vite",
"build": "vite build",
"lint": "eslint .",
"preview": "vite preview"
},
"dependencies": {
"immer": "^11.1.8",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"zustand": "^5.0.14"
"react-dom": "^19.1.0"
},
"devDependencies": {
"@eslint/js": "^9.25.0",
+89 -3
View File
@@ -1,10 +1,96 @@
import Quiz from './components/Quiz'
import React from 'react'
import styles from './App.module.scss'
class Clock extends React.Component {
constructor(props) {
super(props);
this.state = { date: new Date() };
}
componentDidMount() {
this.timerId = setInterval( () => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.timerId)
}
tick() {
this.setState({ date: new Date() })
}
render() {
return <div>{this.state.date.toLocaleTimeString()}</div>
}
}
function Clock2() {
const [date, setDate] = React.useState(new Date());
React.useEffect(()=>{
const timerId = setInterval(()=> setDate(new Date()),1000);
return () => clearInterval(timerId);
},[])
return <div>{date.toLocaleTimeString()}</div>
}
function MouseTracker() {
const [x,setX] = React.useState(0);
const [y,setY] = React.useState(0);
const mouseMoveHandler = event => {
setX(event.clientX);
setY(event.clientY);
}
React.useEffect(()=>{
document.addEventListener('mousemove', mouseMoveHandler);
return () => {
document.removeEventListener('mousemove',mouseMoveHandler);
}
},[]);
return <div className={styles.container}>
<p>Координаты мыши:<br /> X:<b>{x}</b>, Y:<b>{y}</b></p>
</div>
}
function DataFetcher() {
const [posts,setPosts] = React.useState([]);
const [users,setUsers] = React.useState([]);
const getPosts = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await response.json();
setUsers(data.reduce((acc, value) => {
if (!(value.userId in acc))
return [...acc,value.userId]
return acc
}))
console.log(users);
setPosts(data.slice(1,10));
}
React.useEffect(() => {
console.log("Компонент создан");
getPosts();
return () => {console.log("Компонент удален");setPosts([])};
},[])
return <div><ol>{posts.map(post => <li id={post.id}>{post.title}</li>)}</ol></div>
}
function App() {
const [tracker,setTracker] = React.useState(false);
const [dataFetcher,setDataFetcher] = React.useState(false);
return (
<>
<h1>Практика по созданию списка задач</h1>
<Quiz />
<h1>Hello, React!</h1>
<Clock />
<Clock2 />
<button onClick={() => setTracker(!tracker)}>Показать/Скрыть трекер</button>
{ tracker && <MouseTracker /> }
<button onClick={() => setDataFetcher(!dataFetcher)}>Показать/Скрыть посты</button>
{ dataFetcher && <DataFetcher /> }
</>
)
}
+11
View File
@@ -0,0 +1,11 @@
.container {
width: 300px;
height: 200px;
border: 2px dashed grey;
margin: 10px;
text-align: center;
p {
margin-top: 20%;
}
}
-31
View File
@@ -1,31 +0,0 @@
import { useState } from 'react'
const Question = ({question, onSubmit}) => {
const [selectedOption,setSelectedOption] = useState('')
const handleSubmit = () => {
console.log(selectedOption);
if (selectedOption) onSubmit(selectedOption);
}
return (
<div>
<h2>Вопрос {question.id}: {question.text}</h2>
<div>
{question.options.map((option, index)=>(
<label key={index}>
<input
type="radio"
name={`question-${question.id}`}
value={option}
checked={selectedOption === option}
onChange={() => setSelectedOption(option)} />
{option}
</label>
))}
</div>
<button onClick={handleSubmit} >Следующий вопрос</button>
</div>
)
}
export default Question
-35
View File
@@ -1,35 +0,0 @@
import useQuizStore from './store/useQuizStore'
import Question from './Question'
import Result from './Result'
const Quiz = () => {
const {
questions,
currentQuestion,
userAswers,
isFinished,
submitAnswer,
goToNextQuestion,
restartQuiz,
} = useQuizStore();
const handleAnswerSubmit = (anwser) => {
console.log('Yes')
submitAnswer(answer)
if (currentQuestion < questions.length - 1 ) {
goToNextQuestion()
}
}
if(isFinished) {
return (<Result />)
}
return (
<div className="quiz">
<Question
question={questions[currentQuestion]}
onSubmit={handleAnswerSubmit} />
</div>
)
}
export default Quiz
-9
View File
@@ -1,9 +0,0 @@
const Result = ({questions, userAswers, onRestart}) => {
return (
<div>
<h2>Результат</h2>
<button onClick={onRestart}>Начать заново</button>
</div>
)
}
export default Result
-36
View File
@@ -1,36 +0,0 @@
import { create } from 'zustand'
const useQuizStore = create(set => ({
questions: [
{
id:1,
text: 'Сколько будет 2+2?',
options: ['3','4','2'],
correctAnswer: '4',
},
{
id:2,
text: 'Столица России?',
options: ['Москва','Париж','Лондон'],
correctAnswer: 'Москва',
},
],
currentQuestion: 0,
userAnswer:[],
isFinished: false,
goToNextQuestion: () => set(state => ({
currentQuestion: state.currentQuestion + 1,
})),
submitAnswer: answer => set(state => {
const updatedAnswer = [...state.userAnswer, answer];
const isFinished = (state.currentQuestion >= state.questions.length - 1);
return {userAnswer: updatedAnswer, isFinished};
}),/*
restartQuiz: () => set({
currentQuestion: 0,
userAnswer: [],
isFinished: false
}), */
}))
export default useQuizStore
+2
View File
@@ -4,5 +4,7 @@ import { createRoot } from 'react-dom/client'
import App from './App.jsx'
createRoot(document.getElementById('root')).render(
<StrictMode>
<App />
</StrictMode>
)