dmitripavlutin.com | Dmitri Pavlutin 's blog powered by GatsbyJS | Blog library
kandi X-RAY | dmitripavlutin.com Summary
kandi X-RAY | dmitripavlutin.com Summary
Dmitri Pavlutin's blog powered by GatsbyJS
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of dmitripavlutin.com
dmitripavlutin.com Key Features
dmitripavlutin.com Examples and Code Snippets
Community Discussions
Trending Discussions on dmitripavlutin.com
QUESTION
I'm using jest-mock-extended in my tests.
I would like to test the following code:
...ANSWER
Answered 2021-Jun-09 at 17:58I think you should use containsValue('value')
matcher from jest-mock-extended
in eg.ts
file
QUESTION
I'm trying to understand the general advice I've seen regarding React and stale closures.
Specifically, as I understand it, the term "stale closure" is used to describe a scenario where a component and useEffect
function are constructed like this
ANSWER
Answered 2021-Apr-16 at 00:46Am I missing some nuance of javascript's scope here?
No, you're right, creating the array and passing it to useEffect
doesn't affect the closure, the closed-over constant keeps its value.
Or does this "fix" things because of something that
useEffect
is doing with those variables?
Yes. React runs the entire render function each time the state changes, which creates a new closure and passes it to useEffect
again. When the dependencies change, useEffect
re-runs the effect function which creates a new interval with the new closure.
Also, th effect function is returning a cleanup function, which runs when the component unmounts or before running the effect the next time (when the dependencies change). This cleanup function calls clearInterval
, which means the stale closure won't be executed again, and the number of concurrently active intervals doesn't increase.
Admittedly, this proposed solution has a huge bug: clearing the interval and starting a new interval every time the count
changes does not lead to a nice periodic 2s interval, the gaps between two logs might be much larger - the logging is essentially debounced and will only run if no increment happened in the last 2s. If this is not desired, a ref might be a much simpler solution:
QUESTION
import { useEffect, useState } from "react";
function CountSecrets() {
const [secret, setSecret] = useState({ value: "", countSecrets: 0 });
useEffect(() => {
if (secret.value === 'secret') {
setSecret(s => ({...s, countSecrets: s.countSecrets + 1})); }
}, [secret]);
const onChange = ({ target }) => {
setSecret(s => ({ ...s, value: target.value }));
};
return (
Number of secrets: {secret.countSecrets}
);
}
...ANSWER
Answered 2021-Mar-30 at 09:38Thumb rule in React is that state object is immutable, can not change it directly. So whenever you want to update state you always create new objects.
Looking at your code, new objects created in both the cases in onChange
as well as in useEffects
. But useEffects
creates infinite loop as dependency gets changed whereas onChange does not create infinite loop as state update does not onChange
again
if you want to avoid an infinite loop, I suggest 2 ways.
- Change the dependency array from
[secret] --> [secret.value]
- Or have these 2 values
value, countSecrets
as 2 different state
In summary, when you have object as state then you should always create new object and set it. Where as for value based state, react will take care of it.
QUESTION
I'm exporting a function from a controller file:
...ANSWER
Answered 2021-Feb-23 at 01:41This is most probably just a TypeScript syntax issue.
In your adminControllers.ts file, try the export
syntax, like you have done in your adminSaveSnippet file, instead of the CommonJS exports
:
QUESTION
Lately I've been thinking about ways to memoize some children components of a functional component, based on Dmitri's how to use React.memo wisely. These children components may / may not be using some props from the main component.
Let the component be written like so:
...ANSWER
Answered 2021-Feb-01 at 01:56I used the memo in a little experiment, I upload the pictures maybe it will be useful.
Title.js:
App.js:
QUESTION
My preference of choice is to use arrow functions when writing JS.
I read many articles which describe when NOT to use arrow functions, including a question that was answered on StackOverflow:
- when should I use arrow functions in ecmascript-6
- When 'Not' to Use Arrow Functions - Dmitri Pavlutin
- When You Should Not Use Arrow Functions - Javascript Tutorial
All of these articles state that one should not use arrow functions when using eventHandlers since the scope of this
gets set to global instead of the object that was clicked on. However, I have been using arrow functions with event handlers like follows:
ANSWER
Answered 2020-Nov-02 at 15:26However, with so many articles recommending against using arrow functions in event handlers, I am wondering if there are any disadvantages to my approach that I may have missed?
It doesn't look like you have missed anything. You are not using any of the "features" that make arrow functions different from "normal" functions.
You are
- not using
this
- not using
arguments
- not calling the function with
new
(and the browser doesn't either)
so you can choose either form.
If I remember correctly event.target
didn't exist in older versions of Internet Explorer (< 9) (instead it was event.srcElement
), but if you are using an arrow function you probably don't care about deprecated browsers ;)
QUESTION
In the below script would I like to read the content of config.toml
, and have marked where I would like to do that. What confuses me is how to include the toml.js
file which contains the toml reader function.
Throughout the script will I need to read 3 different toml files.
Question
How should I include the toml.js
function and how to reuse the function to read 3 different files?
Disclaimer: I am sorry for this super noob question, but this is my first project, and with 6 different ways to write a function, I find it very confusing.
index.js
...ANSWER
Answered 2020-Apr-21 at 19:44You should be able to call toml('path/to/config/that/you/want/to/read.toml')
You have required a module src/toml.js. This module exports a function - doesn't really matter how this function is declared in this case. Whenever you import that module - you are given this function.
So:
QUESTION
I was reading this article about js functions. https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/
and it says "The function declaration creates a variable in the current scope with the identifier equal to function name. This variable holds the function object."
So I did some experiments to learn more.
...ANSWER
Answered 2018-Dec-06 at 10:45Function declarations are hoisted to the top of their containing function (or the outermost block). Your lower code is equivalent to the following:
QUESTION
I'm new to ReactJs and trying to follow best practices. From my research, I've come across a couple of contradicting articles discussing how implementation should be.
Should state rely on the properties being passed down from a parent component? In the comparisons below, they are both following SRP, but not sure which is best. Would like your advice, Thanks!
1. -- Best Practices for Component State in React.js...First, and probably the most important of all, the state of a component should not depend on the props passed in. (see below for example of what we should not do)
ANSWER
Answered 2018-May-31 at 21:53There is a huge difference between example 1 & 2.
In example #1, the reason it's bad to set state from the those props in that way is that if the props change, the widget will not update. Best practices or not, that is just wrong and bad in any framework. In that particular case, there really is no point in even using the state. Props alone will suffice.
In example #2 the prop is only being used to give the state an initial value (The prop is even named initialValue
), implying that further changes to the state will be controlled by the component regardless of prop changes. It does not break single responsibility principle to use props for an initial state, especially when it's explicitly use for that purpose.
I really don't see those two examples as being contradictory because they are completely different. Further, there is no rule in single responsibility principle that you can't set state from props, you just need to pay attention to the context in which you are doing it.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install dmitripavlutin.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