Практика Zustand
This commit is contained in:
+2
-16
@@ -1,24 +1,10 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import { create } from 'zustand'
|
||||
|
||||
const useUserStore = create(set => ({
|
||||
fetchUser: async userId => {
|
||||
set({ isLoading: true, error: null})
|
||||
try {
|
||||
const response = await fetch(`api/users/${userId}`)
|
||||
const user = await response.json()
|
||||
set({user, isLoading: false})
|
||||
} catch (error) {
|
||||
set({error: error.message, isLoading: false})
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
import Quiz from './components/Quiz'
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<>
|
||||
<h1>Практика по созданию списка задач</h1>
|
||||
<Quiz />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
export function MyComponent() {
|
||||
return (
|
||||
<>
|
||||
<h1>Hello</h1>
|
||||
</>
|
||||
)
|
||||
}
|
||||
export default MyComponent
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user