Show HN: Build your first real world React.js application
academy.plot.ly
Show HN: Build your first real world React.js application
1–10 of 46 posts
Re: Show HN: Build your first real world React.js application
#2Which for any new tool is important. Good luck.
Re: Show HN: Build your first real world React.js application
#3Re: Show HN: Build your first real world React.js application
#4My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post:
switch (action.type) {
case 'CHANGE_LOCATION':
return Object.assign({}, state, {
location: action.location
});
case 'SET_SELECTED_TEMP':
return Object.assign({}, state, {
selected: {
temp: action.temp,
date: state.selected.date
}
});
case 'SET_SELECTED_DATE':
return Object.assign({}, state, {
selected: {
date: action.date,
temp: state.selected.temp
}
});
default:
return state;
}
This reminds me of Win32 event loops with their window message handler switches... And that's a scary association.What's the point with dispatching these fragile "stringly-typed" selectors, when you can just have real functions doing setState() updates on the relevant component?
I'm not trying to be snarky -- I know I'm missing something major here.
Re: Show HN: Build your first real world React.js application
#5Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
On the other hand, you have all the state logic on reducers... If actions were to dispatch state-modifying functions, it wouldn't take long for the code to become really confusing.
Re: Show HN: Build your first real world React.js application
#6Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
Using Redux, all or almost all of your state is stored in a global repository (the "store").
Those strings should probably be constants instead, especially for IDE auto-complete purpose, but you are never generating those manually.
Instead, your components would call "action creators", who are properly named functions, which in turn will dispatch one or several actions to the store.
In your component:
actions.changeLocation(someLocation)
In your action creator: changeLocation(location) {
dispatch({
type: CONSTANTS.CHANGE_LOCATION,
payload: { location },
});
}Re: Show HN: Build your first real world React.js application
#7Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
We have migrated a load of model logic from Angular to redux and redux saga, once you get your head around a nice structure then its a dream.
I created a gist for you to check out [1]. Maybe also check out the concept of Redux Ducks [2]. I now find our code easy to read, share and test - unlike our massive Angular app.
Also as somebody else comments, you should make sure the only way you talk to your store is through a reducer dispatcher.
Regarding component states, find some reading on components v containers [3] - we even have 2 separate folders for the different types. Once mastered and implemented, most components are small and again easy to test.
[1] https://gist.github.com/anonymous/a20f603f9922742bd3cea7a08c...
[2] https://github.com/erikras/ducks-modular-redux
[3] https://medium.com/@dan_abramov/smart-and-dumb-components-7c...
Re: Show HN: Build your first real world React.js application
#8Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
The real power of reducers is composability. For example, I would almost certainly store the "selected" part of the state in a separate reducer. In my personal experience, reducers rarely respond to more than 3-4 actions.
As for the reason to do this instead of setState -- the whole point is to completely encapsulate state into something that's reproducable, trackable, and removable. Almost all of the components you write should be "dumb" -- that is, they just take properties and render some JSX. Then you have a few "smart" components who know about the state and send the data down to it's "dumb" children. Redux/Flux allows your application to be as stateless as possible, which is where React really shines.
Re: Show HN: Build your first real world React.js application
#9Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
Re: Show HN: Build your first real world React.js application
#10Nice tutorials. I've been playing around with React, and I like it overall... But I can't wrap my head around Redux. Could someone explain it to me so I might finally see the light? My problem is this: I just can't understand why I'd want to manipulate state in an unsightly switch statement like this one from Chapter 4 of the original post: switch (action.type) { case 'CHANGE_LOCATION': return Object.assign({}, state…
First, this code could be cleaned up a little. The action types should definitely be constants -- something like ActionTypes.CHANGE_LOCATION. Using lodash's _.defaults is also a lot more terse than Object.assign. And if you're a real stickler for minimalism: https://github.com/acdlite/redux-actions#handleactionsreduce... The real power of reducers is composability. For example, I would almost certainly store the "sel…
case 'CHANGE_LOCATION':
return {
...state,
location: action.location
}