kentcdodds.com | My personal website | Frontend Framework library
kandi X-RAY | kentcdodds.com Summary
kandi X-RAY | kentcdodds.com Summary
My personal website
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Post refresh handler
- This function is called when a commit changes to the current commit
- Retrieve changed files from current commit
- Fetch the json file
- Get the commit object
- Check the status .
kentcdodds.com Key Features
kentcdodds.com Examples and Code Snippets
Community Discussions
Trending Discussions on kentcdodds.com
QUESTION
I am wrapping every routed page in a top level component but the passed in page
is a function (for reasons I cannot export components as is) so, in order to render them, I have to call their functions directly like so:
ANSWER
Answered 2022-Jan-12 at 09:18Creating an element with React.createElement
seems to be doing the trick.
QUESTION
By "React rendering cycle" I mean the process of rendering, reconciliation and committing that's referenced in these blogs.
And by "React component lifecycle" I mean the lifecycle stages of mounting, updating and unmounting that can be observed by developers directly in their components using componentDidMount(), componentWillUnmount(), etc.
Is there any relationship between the two or are these completely different concepts? I've seen a lot more info about the latter rather than the former so is the former something we shouldn't worry about at all?
...ANSWER
Answered 2021-Nov-10 at 08:02as per my knowledge render
is itself a function, that React is offering us, to show our JSX HTML code on the UI screen, thats only method we must define in our class React.Component
thats subclass is render, remains methods are optional not mandatory like render, when we talk about lifecycle it includes everything all methods of the component,
QUESTION
I have an odd situation where a state variable is updating successfully in one component but not in the other. I'm successfully changing the state
, because I can see it reflected in the component where I'm triggering a dispatch
. But in another component that is wrapped in the same Provider
, I see no changes.
Other Stack Overflow answers seem to mostly recommend not directly mutating state, but here I'm not doing that - I'm dispatching an action to update the state, similar to what you'd see in Redux syntax.
TeaSettingsContext.tsx
...ANSWER
Answered 2021-Oct-22 at 16:50I think you misunderstood a little how context works.
Basically (as react docs suggest):
Every Context object comes with a Provider React component that allows consuming components to subscribe to context changes.
The Provider component accepts a value prop to be passed to consuming components that are descendants of this Provider. One Provider can be connected to many consumers. Providers can be nested to override values deeper within the tree.
All consumers that are descendants of a Provider will re-render whenever the Provider’s value prop changes. The propagation from Provider to its descendant consumers (including .contextType and useContext) is not subject to the shouldComponentUpdate method, so the consumer is updated even when an ancestor component skips an update.
On short when you create a provider with a value you are able to access that information from multiple consumers (that need to be children of that provider). So far so good.
Except:
Home.tsx
and TeaPage.tsx
both initialize a different instance of the context (so each one has it's own provider).
In order to fix this you should only create one provider in a component that is parent of both Home
and TeaPage
.
React context is not working as redux (where you have a global store that you can access from everywhere). It rather give you the option to create a context (with reusable data) for a big component (to consume in the children).
Also please notice the last quoted paragraph from the beginning: basically when the context changes this cause all consumers to re-render (which might sometimes lead to some performance issues) so use it wisely. Here is a short article about this.
QUESTION
This is a more concise version of a question I raised previously. Hopefully, it's better explained and more understandable.
Here's a small app that has 3 inputs that expect numbers (please disregard that you can also type non-numbers, that's not the point). It calculates the sum of all displayed numbers. If you change one of the inputs with another number, the sum is updated.
Here's the code for it:
...ANSWER
Answered 2021-Sep-20 at 19:38As you can see, when a single input is updated, all the children are not re-rendered, they are first unmounted, then re-mounted. What a waste, all the input are already in the right state, only the sum needs to be updated. And imagine having hundreds of those inputs.
No, the logs you see from the useEffect
don't represent a component mount/unmount. You can inspect the DOM and verify that only one input is updated even though all three components get rerendered.
If it was just a matter of re-rendering, I could look at memoization. But that wouldn't work because the callback is updated precisely because items change. No, my question is about the unmounting of all the children.
This is where you would use a functional state update to access the previous state and return the new state.
QUESTION
I'm following an action/reducer pattern for React put forth by Kent Dodds and I'm trying to add some type safety to it.
...ANSWER
Answered 2021-Sep-16 at 01:37type ActionDataMap = {
DO_SOMETHING: { num: Number };
DO_SOMETHING_ELSE: { nums: Number[] }
};
type ActionType = keyof ActionDataMap
type ActionsMap = {
[K in ActionType]: { type: K; data: ActionDataMap[K] }
}
// this will generate the union:
type Action = ActionsMap[ActionType]
// you can index into ActionsMap with K to find the specific Action
type Actions = {
[K in ActionType]: (state: State, action: ActionsMap[K]) => State;
};
QUESTION
I'm trying to set up tests for a React-Redux application. Most resources including Redux Docs, and the main contributor to react-testing-library argues for a focus on integration tests and that the Redux code as far as possible should be considered an implementation detail. That's cool and all, but I'm struggling to do this in practice.
Most examples I've found on integration testing sets up a component where you can observe the effect in the immediate unit. But several places in my app, I have components that send dispatches to my store which causes a re-render in another part of the app. To illustrate my question, I've made a simplified component tree below where I want to click the AddToListButton
to cause Element1
to be added to the List
.
If Redux is an implementation detail, how do I actually test that clicking the button causes the update without rendering (more or less) the whole app? I understand how to do unit-testing and end-to-end testing, but not sure how I would implement integration testing.
...ANSWER
Answered 2021-Aug-27 at 08:36You would actually render (more or less) the whole app - or at least all involved components. That is pretty much the point of integration testing.
The only difference to end-to-end testing is that no other services are involved and you can also integration-test only parts of your app, but that does not mean that you have to integration-test only parts of your app.
QUESTION
I've created a sandbox using useMemo to optimize a mock expensive function follow Kent C Dodds example from this post. Memoisation doesn't seem to be working. Any ideas why?
...ANSWER
Answered 2021-Aug-11 at 09:19useMemo() is actually working correctly in your code
You have:
const value = useMemo(() => wait(duration), [duration]);
useMemo()
recalculate its value every time any of the values from its dependency changes, and you have [duration]
inside your dependency array which change every time, you click the setDuration()
button.
If you want to see useMemo()
works, make the component re-render without changing its dependency.
Something like this: useMemo()
QUESTION
I have a basic login form made using Formik, just trying to write an RTL and jest code to fill out the details, submit the form, and navigate to the Home page. Therefore, wrote the following:
...ANSWER
Answered 2021-Jul-12 at 13:17I managed to pass the test case with no act warnings.
Since you are calling an setTimeout which is an asynchronous function when the user clicks on the submitBtn
button, therefore you have to wait for the promise to resolve.
QUESTION
SOLUTION: https://kentcdodds.com/blog/compound-components-with-react-hooks
I am creating a component and I would like to displays a warning only if or are not a child of .
I already made a thing to resolve the issue but I am not satisfied, I think this is ugly.
...ANSWER
Answered 2021-May-21 at 17:31In short, you're only going 1 level deep within the children. You'll have to recursively traverse through the children of children to assign the isInCard
property.
React composes its children like a tree:
QUESTION
I am rendering the component in an integration test of my single page app and attempting to follow a happy path from landing on a page to the first question of a quiz / memory game.
...ANSWER
Answered 2021-Mar-23 at 23:09Here are several things you can do:
- The warning tells you that your app continues after your test completed, and thus other things happen during the run that you didn't check in your test and didn't wait for. So if you're happy with what you are testing, be warned and keep it like that.
- You can make your app a bit more testable by giving it options. For example you can have an optional prop indicating the amount of time to wait in timers, so that you can put it to 0 or a small number in your tests.
- You can mock timers:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install kentcdodds.com
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page