Skip to content

Commit f815714

Browse files
committed
Refactor QuestionContext inside App
- WIP due to error Uncaught TypeError: create is not a function
1 parent 0a8f6fb commit f815714

File tree

6 files changed

+27
-38
lines changed

6 files changed

+27
-38
lines changed

src/App.jsx

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
import React, { useState, useEffect, useContext } from 'react';
1+
import React, { useState, useEffect, createContext } from 'react';
22
import './App.css';
33
import Home from './components/Home'
44
import Quiz from './components/Quiz'
55
import Results from './components/Results'
66
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
77
import axios from 'axios'
8-
import { TriviaGameProvider, QuestionsContext } from './context/TriviaGameContext'
8+
// import { TriviaGameProvider } from './context/TriviaGameContext'
99

10+
const QuestionsContext = createContext({
11+
question: [],
12+
setQuestions: () => []
13+
});
1014

1115
const App = () => {
1216

13-
const [questions, setQuestions] = useContext(QuestionsContext)
17+
const [questions, setQuestions] = useState([])
1418

1519
// useEffect make axios request to fetch the quesiton data
1620
useEffect(() => {
@@ -22,10 +26,12 @@ const App = () => {
2226
})
2327
.catch( err => console.error(err))
2428
}, [])
29+
30+
console.log(questions)
2531

2632
return (
27-
<Router>
28-
<TriviaGameProvider>
33+
<QuestionsContext.Provider value={{questions, setQuestions}}>
34+
<Router>
2935
<main className="App">
3036
<Switch>
3137
<Route path="/quiz">
@@ -39,9 +45,9 @@ const App = () => {
3945
</Route>
4046
</Switch>
4147
</main>
42-
</TriviaGameProvider>
43-
</Router>
48+
</Router>
49+
</QuestionsContext.Provider>
4450
);
4551
}
4652

47-
export default App
53+
export { App, QuestionsContext}

src/components/ButtonBox.jsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import React, { useState, useEffect, useContext} from 'react'
22
import styled from 'styled-components'
33
import { Link } from 'react-router-dom'
4-
import { QuestionsContext } from '../context/TriviaGameContext'
4+
import { QuestionsContext } from '../App'
55

66
export default ({handleTrueClick, handleFalseClick, allClicks}) => {
77
const [isComplete, setIsComplete] = useState(false)
88
const questions = useContext(QuestionsContext)
99
const [selected, setSelected] = useState(0)
1010

11+
12+
const calculateScore = (answers) => {
13+
questions[selected].map(item => {
14+
return console.log(item)
15+
})
16+
console.log('You ran the calculate score effect')
17+
}
1118
useEffect(
1219
(allClicks.length > 9) ?
1320
setIsComplete(true)
1421
.then(calculateScore(allClicks))
1522
: ''
1623

1724
, [allClicks])
18-
19-
const calculateScore = (answers) => {
20-
questions[selected].map(item => {
21-
console.log(item)
22-
})
23-
console.log('You ran the calculate score effect')
24-
}
2525
return(
2626
<>
2727
<ButtonBox>
2828
{/* ternary checks if completed quiz through question 10 yet*/}
2929
{(isComplete) ?
3030
// if question 10 complete show results button
3131
<>
32-
<Link to="/results?answers=${scoreCard}">
32+
<Link to="/results">
3333
<Button>results</Button>
3434
</Link>
3535
</>

src/components/Quiz.jsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useContext } from 'react'
22
import Header from './Header'
33
import Card from './Card'
44
import ButtonBox from './ButtonBox'
5-
import { QuestionsContext } from '../context/TriviaGameContext'
5+
import { QuestionsContext } from '../App'
66

77
const Quiz = () => {
88
const [selected, setSelected] = useState(0)
@@ -14,6 +14,7 @@ const Quiz = () => {
1414
- create True or False in the allClicks store array
1515
- increment to the next question's data
1616
**/
17+
1718
const handleTrueClick = () => {
1819
setAllClicks(allClicks.concat('True'))
1920
if(allClicks.length < 9){

src/components/Results.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState, useContext } from 'react'
22
import Header from './Header'
33
import styled from 'styled-components'
44
import { Link } from 'react-router-dom'
5-
import { QuestionsContext } from '../context/TriviaGameContext'
5+
import { QuestionsContext } from '../App'
66

77

88
const Results = ({ allClicks}) => {

src/context/TriviaGameContext.jsx

-18
This file was deleted.

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
4-
import App from './App';
4+
import { App } from './App';
55

66
ReactDOM.render(
77
<React.StrictMode>

0 commit comments

Comments
 (0)