Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
실전: action을 dispatch를 통해서 전달하기
Browse files Browse the repository at this point in the history
  • Loading branch information
mjun0328 committed Sep 14, 2023
1 parent 48a88cd commit 1c0be68
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion main.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ <h1>WEB</h1>
const state = store.getState();
let liTags = "";
for (let i = 0; i < state.contents.length; i++) {
liTags += `<li><a href="${state.contents[i].id}.html">${state.contents[i].title}</a></li>`;
liTags += `
<li>
<a onclick="
event.preventDefault();
const action = {type: 'SELECT', id: ${state.contents[i].id}};
store.dispatch(action);
"
href="${state.contents[i].id}.html">
${state.contents[i].title}
</a>
</li>
`;
}
document.querySelector("#toc").innerHTML = `
<nav>
Expand Down Expand Up @@ -53,6 +64,7 @@ <h2>HTML</h2>
function reducer(state, action) {
if (state === undefined) {
return {
selected_id: null,
contents: [
{
id: 1,
Expand All @@ -67,6 +79,13 @@ <h2>HTML</h2>
],
};
}

let newState;
if (action.type === "SELECT") {
newState = Object.assign({}, state, { selected_id: action.id });
}
console.log(action, state, newState);
return newState;
}
const store = Redux.createStore(reducer);
subject();
Expand Down

0 comments on commit 1c0be68

Please sign in to comment.