You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
there seems to be some errors here and apparently the lab hasn't been updated even though problems have been noted since at least September. Below might not be the perfect solution but this is how I get the lab to actually run.
Line 13: dispatch({type: '@@init'}) leaving this here throws a store not defined error - the store is created on Line 37 so calling this for the first time within createStore seems to throw an error that I think is calling dispatch on a store that hasn't initialized yet.
To fix this error i called store.dispatch({type: '@@init'}) outside of the createStore function, directly on line 38 (after the store = createStore function) This allows for the DOM to show a state of zero on a page reload.
But for the DOM to display at all we need to fix the Render function. It doesn't render anything because you need to call the function on line 34 - it should be store.getState() which would give you {count: number}...so store.getState().count gives you the number for the DOM
function createStore(reducer) {
let state;
function dispatch(action) {
state = reducer(state, action);
render();
}
Flatiron team, I would really like to see a working solution with store.dispatch({type: '@@init'}) kept inside of our new function because the main focus of this lesson is to "move code common to every javascript application inside our new function" . The lesson shows this approach, but the code does not work.
Thank you for spotting this issue and providing feedback.
We have updated the materials and believe your issue to have been resolved.
We apologize for any frustration you might have encountered during this process but thank you for helping us ensure that those who follow in your path will not encounter the same problems that you faced.
If you do not believe that this issue has been addressed, please re-open this issue. 💙
there seems to be some errors here and apparently the lab hasn't been updated even though problems have been noted since at least September. Below might not be the perfect solution but this is how I get the lab to actually run.
Line 13: dispatch({type: '@@init'}) leaving this here throws a store not defined error - the store is created on Line 37 so calling this for the first time within createStore seems to throw an error that I think is calling dispatch on a store that hasn't initialized yet.
To fix this error i called store.dispatch({type: '@@init'}) outside of the createStore function, directly on line 38 (after the store = createStore function) This allows for the DOM to show a state of zero on a page reload.
But for the DOM to display at all we need to fix the Render function. It doesn't render anything because you need to call the function on line 34 - it should be store.getState() which would give you {count: number}...so store.getState().count gives you the number for the DOM
function createStore(reducer) {
let state;
function dispatch(action) {
state = reducer(state, action);
render();
}
function getState() {
return state;
};
return {
dispatch,
getState
};
};
function changeCount(state = { count: 0 }, action) {
switch (action.type) {
case 'INCREASE_COUNT':
return { count: state.count + 1 };
}
}
function render() {
let container = document.getElementById('container');
container.textContent = store.getState().count;
};
let store = createStore(changeCount) // createStore takes the changeCount reducer as an argument
store.dispatch({type: '@@init'})
let button = document.getElementById('button');
button.addEventListener('click', function() {
store.dispatch({ type: 'INCREASE_COUNT' });
});
The text was updated successfully, but these errors were encountered: