learn-co-curriculum / redux-create-store Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
state isn't accessible with the example render function below.
function render() {
let container = document.getElementById('container');
container.textContent = store.getState.count;
};
instead I had to invoke store.getState function first then access .count to render current state to DOM.
Thank you!
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' });
});
The solution code passes the test but doesn't work in the browser. When I paste in the solution code and run open index.html
, the console presents me with this error Uncaught SyntaxError: Unexpected identifier
. Please provide a better lab.
I was following alone and the final code is giving an error:
ReferenceError: store is not defined
at render (reducer_old.js:34)
at dispatch (reducer_old.js:6)
at createStore (reducer_old.js:13)
at reducer_old.js:37
Please fix.
Not necessarily a big deal since you can just commit and push with regular Git commands, but for some reason the 'learn submit' command doesn't work. Not sure why this might be since all the previous codealongs in this section work fine and look the same. Only thing that I see that's different is the version in the package.json file (this one is 0.1.0 vs. 1.0.0 in the previous). Here's the error message on attempting to submit:
I copied and pasted the code and get the error that within render() store is not defined.
In the previous README, it was function reducer
but now it's back to function changeCount
Use js/reducer.js to follow along. Open index.html to try out the code.
reducer.js should be createStore.js, as that is the main code file in the repo.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.