https://gazero-mbti.netlify.app/
npx create-react-app mbti-test
npm install react-redux redux
npm install styled-components
npm start
I/E, S/N, T/F, P/J 중
각각 하나씩
E S T J의 포인트 점수를 조정하는 방식
예를들어, '그렇다'를 누르면 E포인트 INCREASE상태가 되고,
'아니다'를 누르면 E포인트 DECREASE상태로 점수를 조절
const initialState = {
isTestBegin: false,
questionCount: 0,
ePoint: 0,
sPoint: 0,
tPoint: 0,
jPoint: 0,
};
function reducer(currentState, action) {
///여기에 들어갈 조건들///
}
import { createStore } from "redux";
/* ...초기상태
...리듀서함수*/
export const store = createStore(reducer);
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>
import { useDispatch } from "react-redux";
const Home = () => {
const dispatch = useDispatch();
return (
<StButton onClick={() => dispatch({ type: "BEGIN" })}>
);
};
export default Home;
import { useSelector, useDispatch } from "react-redux";
const Question = (props) => {
const questionCount = useSelector((state) => state.questionCount);
return (
<Title>
<TitleNum>{questioncontent[questionCount].number}</TitleNum>
<h1>{questioncontent[questionCount].question}</h1>
</Title>
);
};
export default Question;
import { useSelector, useDispatch } from "react-redux";
const Question = (props) => {
const dispatch = useDispatch();
return (
<OkButton
onClick={() =>
dispatch({
type: "INCREASE",
quizType: questioncontent[questionCount].type,
})
}
>
<span> {questioncontent[questionCount].answer1} </span>
</OkButton>
<br />
<OkButton
onClick={() =>
dispatch({
type: "DECREASE",
quizType: questioncontent[questionCount].type,
})
}
>
);
};
export default Question;
const Game = () => {
const questionCount = useSelector((state) => state.questionCount);
return (
<div style={{ textAlign: "center" }}>
{questionCount === 16 ? <Result /> : <Question />}
</div>
);
};
export default Game;
const ePoint = useSelector((state) => state.ePoint);
const sPoint = useSelector((state) => state.sPoint);
const tPoint = useSelector((state) => state.tPoint);
const jPoint = useSelector((state) => state.jPoint);
const [resultCharacter, setCharacter] = useState({});
useEffect(() => {
let result = "";
result += ePoint > 0 ? "E" : "I";
result += sPoint > 0 ? "S" : "N";
result += tPoint > 0 ? "T" : "F";
result += jPoint > 0 ? "J" : "P";
const finalResult = resultcontent.filter((content) => {
return content.type === result;
});
setCharacter(...finalResult);
}, []);
<StButton onClick={() => dispatch({ type: "RESET" })}>RETRY</StButton>