Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The errors in this lab really should be fixed #12

Closed
bmolina-nyc opened this issue Dec 20, 2017 · 2 comments
Closed

The errors in this lab really should be fixed #12

bmolina-nyc opened this issue Dec 20, 2017 · 2 comments

Comments

@bmolina-nyc
Copy link

bmolina-nyc commented Dec 20, 2017

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 };

default:
  return state;

}
}

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' });
});

@gb23
Copy link

gb23 commented Jan 7, 2018

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.

@maxwellbenton
Copy link
Contributor

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. 💙

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@bmolina-nyc @gb23 @maxwellbenton and others