Skip to content

Copy code from cra-template-redux and convert to TS - #3

Merged
markerikson merged 14 commits into
reduxjs:masterfrom
BenLorantfy:master
Feb 28, 2020
Merged

Copy code from cra-template-redux and convert to TS#3
markerikson merged 14 commits into
reduxjs:masterfrom
BenLorantfy:master

Conversation

@BenLorantfy

@BenLorantfy BenLorantfy commented Feb 21, 2020

Copy link
Copy Markdown
Contributor

Copies over the code from cra-template-redux and converts it to TS.

A few notes:

  • Not sure if I did the typescript correctly for the selector
  • I didn't bother copying over the jest/babel configuration. Figure we can set that up if/when we want to setup CI.

Screen Shot 2020-02-20 at 11 59 44 PM

Comment thread template.json Outdated
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>React Redux App</title>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we change the title of the JS one too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread template/src/App.tsx Outdated
Comment thread template/src/features/counter/counterSlice.ts Outdated
Comment thread template/src/features/counter/counterSlice.ts Outdated
Comment thread template/src/store.ts Outdated
Comment thread template/src/store.ts Outdated
@nickserv

Copy link
Copy Markdown
Contributor

Sidenote: I think it would be easier to maintain this as a fork of cra-template-redux so we can pull updates from upstream, otherwise we'd have to duplicate them manually

@markerikson

Copy link
Copy Markdown
Contributor

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.

@markerikson

Copy link
Copy Markdown
Contributor

Just left some thoughts on improvements I want made to the JS template.

Pasting them here:

After trying this out, a few thoughts for tweaks:

  • It would be good to either show use of an inline selector with useSelector, or add a comment right above the useSelector saying you can define them inline
  • I'd like to tweak the slice comment slightly and say:
      // Redux Toolkit allows us write "mutating" logic in reducers. It doesn't 
      // actually mutate the state because it uses the immer library, which detects
      // changes to a "draft state" and produces a brand new immutable state
      // based off those changes
  • We should have an example of a thunk somewhere. Simplest example would be an "Add Async" button that sleeps for 1 second and then dispatches increment().
  • I want to move store.js into an app subfolder.

These changes should also be made to the TS template as well.

export function Counter() {
const count = useSelector(selectCount);
const dispatch = useDispatch();
const [incrementAmount, setIncrementAmount] = useState('2');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be a number, not a string

Suggested change
const [incrementAmount, setIncrementAmount] = useState('2');
const [incrementAmount, setIncrementAmount] = useState(2);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I keep this a number, I get typescript errors when I use setIncrementAmount in the input below

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be kept as a number in state, since that's what the data type represents logically.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I want both templates consistent in terms of actual code as much as possible.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it can be a number? to allow for the empty case

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or keep as string and use it to demonstrate simple validation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread template/src/react-app-env.d.ts Outdated
Comment thread template/tsconfig.json Outdated
Comment thread package.json
Comment thread package.json
Comment thread template/src/store.ts
import counterReducer from './features/counter/counterSlice';

export default configureStore({
export const store = configureStore({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export const store = configureStore({
export default configureStore({

We should keep this as export default because its default import is used

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd actually prefer to make it a named export in both templates.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@BenLorantfy BenLorantfy Feb 22, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the import bug though, good catch

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer default exports but I could go either way, I just wanted to point out that bug

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, let's leave App as is, and try to go for named exports pretty much everywhere else.

Comment thread template/src/features/counter/Counter.tsx
@BenLorantfy

BenLorantfy commented Feb 22, 2020

Copy link
Copy Markdown
Contributor Author

Just left some thoughts on improvements I want made to the JS template.

Should we make these changes in this PR or a separate one? Might be easier to make in a separate PR

@BenLorantfy

Copy link
Copy Markdown
Contributor Author

@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 😅

@mamal72

mamal72 commented Feb 22, 2020

Copy link
Copy Markdown

@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

@nickserv

Copy link
Copy Markdown
Contributor

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

Comment thread template.json Outdated
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action: PayloadAction<{ amount: number }>) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question for both templates. Do we want to skip having a wrapper object, and just have the value itself be action.payload ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good example of Immer though

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just talking about the action.payload contents - the state.value += part is fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yea, might as well just be a number

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@BenLorantfy

Copy link
Copy Markdown
Contributor Author

@markerikson @nickmccurdy I think I've fixed/responded to everything out-standing my-side. Let me know if I missed something.

@markerikson

Copy link
Copy Markdown
Contributor

@BenLorantfy : are you planning to deal with the items in #3 (comment) in a follow-up PR, or this one?

@BenLorantfy

BenLorantfy commented Feb 28, 2020

Copy link
Copy Markdown
Contributor Author

I think it would make sense to do that stuff in a follow-up PR, since it needs to be done to both the TS and JS one. Either I or @lukyth could do it (since he expressed interest in #1)

@markerikson

Copy link
Copy Markdown
Contributor

Okay, just want to make sure it's not being missed.

In that case, I think this is probably good atm.

@markerikson
markerikson merged commit b967476 into reduxjs:master Feb 28, 2020
@nickserv

Copy link
Copy Markdown
Contributor

Sweet, is this ready to be released or are we waiting on the follow-up?

@markerikson

Copy link
Copy Markdown
Contributor

Waiting for a follow-up per the last couple comments. Feel free to toss that one in :)

@markerikson

Copy link
Copy Markdown
Contributor

Can someone go ahead and make the requested changes to both the TS and JS templates?

After trying this out, a few thoughts for tweaks:

  • It would be good to either show use of an inline selector with useSelector, or add a comment right above the useSelector saying you can define them inline
  • I'd like to tweak the slice comment slightly and say:
      // Redux Toolkit allows us write "mutating" logic in reducers. It doesn't 
      // actually mutate the state because it uses the immer library, which detects
      // changes to a "draft state" and produces a brand new immutable state
      // based off those changes
  • We should have an example of a thunk somewhere. Simplest example would be an "Add Async" button that sleeps for 1 second and then dispatches increment().
  • I want to move store.js into an app subfolder.

@BenLorantfy

Copy link
Copy Markdown
Contributor Author

I can do it, I was just waiting in case someone else really wanted to

@markerikson

Copy link
Copy Markdown
Contributor

Go for it :)

HeatherD2025 referenced this pull request in HeatherD2025/QwRky Jan 23, 2026
Copy code from cra-template-redux and convert to TS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants