Copy code from cra-template-redux and convert to TS - #3
Conversation
| Learn how to configure a non-root public URL by running `npm run build`. | ||
| --> | ||
| <title>React App</title> | ||
| <title>React Redux App</title> |
There was a problem hiding this comment.
Did we change the title of the JS one too?
There was a problem hiding this comment.
|
Sidenote: I think it would be easier to maintain this as a fork of |
|
I'm not expecting the TS template to have that many changes over time, so I don't think we need to treat this as a true repo fork. If that proves wrong, we can rethink it. |
|
Just left some thoughts on improvements I want made to the JS template. Pasting them here:
|
| export function Counter() { | ||
| const count = useSelector(selectCount); | ||
| const dispatch = useDispatch(); | ||
| const [incrementAmount, setIncrementAmount] = useState('2'); |
There was a problem hiding this comment.
Should be a number, not a string
| const [incrementAmount, setIncrementAmount] = useState('2'); | |
| const [incrementAmount, setIncrementAmount] = useState(2); |
There was a problem hiding this comment.
If I keep this a number, I get typescript errors when I use setIncrementAmount in the input below
There was a problem hiding this comment.
Right, it needs to be converted from a string to a number somewhere. Either keep it as a string in the component and convert to a number right before you dispatch, or convert it to a number in the change handler and store it as a number.
There was a problem hiding this comment.
I think it should be kept as a number in state, since that's what the data type represents logically.
There was a problem hiding this comment.
If I keep it as a number in the state, it kind of messes with what the user can type into the textbox. For example, you can't backspace because it converts an empty string into "0".
Currently it gets converted to a number right before the dispatch.
There was a problem hiding this comment.
I think we should change the JS template to be consistent. JS doesn't have an issue currently at run-time because it changes it to a string once the user starts typing.
There was a problem hiding this comment.
Right, I want both templates consistent in terms of actual code as much as possible.
There was a problem hiding this comment.
I wonder if it can be a number? to allow for the empty case
There was a problem hiding this comment.
Or keep as string and use it to demonstrate simple validation
There was a problem hiding this comment.
I think the simplest thing to do is just keep it as a string. Even number? will have problems, because it will restrict what the user can type, which could be confusing. e.g. I can't type 1. if I want to increment by a fractional number.
| import counterReducer from './features/counter/counterSlice'; | ||
|
|
||
| export default configureStore({ | ||
| export const store = configureStore({ |
There was a problem hiding this comment.
| export const store = configureStore({ | |
| export default configureStore({ |
We should keep this as export default because its default import is used
There was a problem hiding this comment.
I'd actually prefer to make it a named export in both templates.
There was a problem hiding this comment.
Personally I don't think it makes sense since the file is named store and this is the main export. However, the other file would still need to be updated to fix the import.
There was a problem hiding this comment.
Right, the import/export syntax needs to be consistent either way.
Overall, I'm just generally leaning away from default exports in most cases in my own code.
There was a problem hiding this comment.
I dislike default exports because of the poor auto-complete experience, but that's just my opinion. We are using default exports in other files though (see App.tsx) so it might be good to use default export here for consistency? Not sure
There was a problem hiding this comment.
I fixed the import bug though, good catch
There was a problem hiding this comment.
I prefer default exports but I could go either way, I just wanted to point out that bug
There was a problem hiding this comment.
@markerikson Do you care about the consistency here? I changed it to a named export but now it's inconsistent with some places where we do default exports
There was a problem hiding this comment.
There was a problem hiding this comment.
Eh, let's leave App as is, and try to go for named exports pretty much everywhere else.
Should we make these changes in this PR or a separate one? Might be easier to make in a separate PR |
|
@markerikson @nickmccurdy I think I replied/corrected to everything. Feel free to resolve whatever comments you're happy with so it's easier to see what's unresolved 😅 |
|
@BenLorantfy TS 3.8 is released and it looks like this project is depending on 3.7.2. I think it might be a good idea to update dependencies here too if you feel like to do it. 🤔 cc: @markerikson @nickmccurdy |
|
For some reason I can't mark my review comments as resolved in this pull request, but you can resolve what's been fixed so far |
| decrement: state => { | ||
| state.value -= 1; | ||
| }, | ||
| incrementByAmount: (state, action: PayloadAction<{ amount: number }>) => { |
There was a problem hiding this comment.
Question for both templates. Do we want to skip having a wrapper object, and just have the value itself be action.payload ?
There was a problem hiding this comment.
I think this is a good example of Immer though
There was a problem hiding this comment.
I'm just talking about the action.payload contents - the state.value += part is fine.
There was a problem hiding this comment.
Oh yea, might as well just be a number
There was a problem hiding this comment.
Might be a good idea but I wonder if we should do that in a separate follow-up PR that we do to both templates
There was a problem hiding this comment.
It's nice to have an example of a wrapper object though, even if you wouldn't necessarily use one for this particular case. But you are trying to cut down on boilerplate with the toolkit, so I can see both ways
|
@markerikson @nickmccurdy I think I've fixed/responded to everything out-standing my-side. Let me know if I missed something. |
|
@BenLorantfy : are you planning to deal with the items in #3 (comment) in a follow-up PR, or this one? |
|
Okay, just want to make sure it's not being missed. In that case, I think this is probably good atm. |
|
Sweet, is this ready to be released or are we waiting on the follow-up? |
|
Waiting for a follow-up per the last couple comments. Feel free to toss that one in :) |
|
Can someone go ahead and make the requested changes to both the TS and JS templates?
|
|
I can do it, I was just waiting in case someone else really wanted to |
|
Go for it :) |
Copy code from cra-template-redux and convert to TS
Copies over the code from cra-template-redux and converts it to TS.
A few notes: