Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 82fb0d8030 | |||
| cc79d1956b | |||
| 55c4ae7d53 | |||
| de33066cbe |
Generated
+44
-3
@@ -8,8 +8,10 @@
|
||||
"name": "store-client",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"immer": "^11.1.8",
|
||||
"react": "^19.1.0",
|
||||
"react-dom": "^19.1.0"
|
||||
"react-dom": "^19.1.0",
|
||||
"zustand": "^5.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
@@ -1367,7 +1369,7 @@
|
||||
"version": "19.1.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/react/-/react-19.1.6.tgz",
|
||||
"integrity": "sha512-JeG0rEWak0N6Itr6QUx+X60uQmN+5t3j9r/OVDtWzFXKaj6kD1BwJzOksD0FF6iWxZlbE1kB0q9vtnU2ekqa1Q==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"csstype": "^3.0.2"
|
||||
@@ -1619,7 +1621,7 @@
|
||||
"version": "3.1.3",
|
||||
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz",
|
||||
"integrity": "sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==",
|
||||
"dev": true,
|
||||
"devOptional": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/debug": {
|
||||
@@ -2054,6 +2056,16 @@
|
||||
"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",
|
||||
@@ -2794,6 +2806,35 @@
|
||||
"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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-2
@@ -4,14 +4,16 @@
|
||||
"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"
|
||||
"react-dom": "^19.1.0",
|
||||
"zustand": "^5.0.14"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/js": "^9.25.0",
|
||||
|
||||
+3
-104
@@ -1,111 +1,10 @@
|
||||
import React from 'react'
|
||||
|
||||
class ErrorBoundary extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {hasError: false};
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasError: true }
|
||||
}
|
||||
|
||||
componentDidCatch(error,info) {
|
||||
console.log(error,info);
|
||||
}
|
||||
|
||||
handlerReset = () => {
|
||||
this.setState({hasError: false});
|
||||
}
|
||||
|
||||
render() {
|
||||
if (this.state.hasError) {
|
||||
return (
|
||||
<div>
|
||||
<h1>Что то поломалось</h1>
|
||||
<button onClick={this.handlerReset}>Повторить</button>
|
||||
</div>)
|
||||
}
|
||||
return this.props.children;
|
||||
}
|
||||
}
|
||||
|
||||
class AdvancedErrorBoundary extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
hasError: false,
|
||||
error: null,
|
||||
errorInfo: null
|
||||
}
|
||||
}
|
||||
|
||||
static getDerivedStateFromError(error) {
|
||||
return { hasErorr: true }
|
||||
}
|
||||
|
||||
componentDidCatch(error, info) {
|
||||
this.setState({
|
||||
error: error,
|
||||
errorInfo: info
|
||||
})
|
||||
}
|
||||
|
||||
handleReset = () => {
|
||||
this.setState({
|
||||
hasError:false,
|
||||
error: null,
|
||||
errorInfo:null
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
if(this.state.hasError) {
|
||||
console.log('Error');
|
||||
return (
|
||||
<div>
|
||||
<h2>Произошла ошибка</h2>
|
||||
<p>Извеняемся, работаем, исправляем. Разработчика привязали к батарее</p>
|
||||
<button onClick={this.handleReset}>Попробовать снова</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
//Если ошибок нет, то рендрим дочерние объекты
|
||||
return this.props.children
|
||||
}
|
||||
}
|
||||
|
||||
function ProblemComponent() {
|
||||
const [counter,setCounter] = React.useState(0)
|
||||
|
||||
const handlerClick = () => setCounter(counter + 1)
|
||||
|
||||
React.useEffect(() => {
|
||||
if (counter > 5) {
|
||||
throw new Error("Ошибка в компоненте");
|
||||
}
|
||||
},[counter]);
|
||||
return (
|
||||
<div>
|
||||
<h2>Счетчик: {counter}</h2>
|
||||
<button onClick={handlerClick}>Прибавить</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
import Quiz from './components/Quiz'
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<>
|
||||
<h1>Обработка ошибок в компонентах</h1>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<ErrorBoundary>
|
||||
<ProblemComponent />
|
||||
</ErrorBoundary>
|
||||
<h1>Практика по созданию списка задач</h1>
|
||||
<Quiz />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
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
|
||||
@@ -0,0 +1,35 @@
|
||||
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
|
||||
@@ -0,0 +1,9 @@
|
||||
const Result = ({questions, userAswers, onRestart}) => {
|
||||
return (
|
||||
<div>
|
||||
<h2>Результат</h2>
|
||||
<button onClick={onRestart}>Начать заново</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
export default Result
|
||||
@@ -0,0 +1,36 @@
|
||||
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
|
||||
@@ -4,7 +4,5 @@ import { createRoot } from 'react-dom/client'
|
||||
import App from './App.jsx'
|
||||
|
||||
createRoot(document.getElementById('root')).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user