Category Archives: Javascript

Npm tips

To update all dependencies in a package.json to their latest versions.. can install this globally first(just once):

and then run below which will update the versions of each dependency to the latest

finally run below to update all of the scripts to their latest. Note, this could be code breaking depending on version updates for your dependencies.

 

Notes for webpack configs to output single files for js and css bundles and more

I often use a site to build webpack configs https://createapp.dev/ and I normally like to have my js and css compiled to one file each within /dist

Out of the box, it handles the entry/output for the .js but a little bit needs to be added to do the same for the .css

Here is an example webpack.config.js

Some important parts added to have the css compile to a single file. First is that the scss file(s) need to be added as an entry:

Secondly, the MiniCssExtractPlugin needs to have an object with the destination css file name added to it as you see here:

 

To process es6+ features like async/await

We must add the below dependency:

Finally, alter the .babelrc to look like :

 

Reactjs using recoil.js for state management notes

Recoil.js is being designed by facebook apparently to replace redux so I figured I’d take a look.

My sample Codesandbox

Atoms  – most basic for of state that can be shared amongst components and or selectors. It requires a “key” which must be unique and to initialize with a value(s) use default.

 

useRecoilState – Used to read and write an atom from a component.

Selectors – a pure function that will basically just return a computed value based on atom(s) and/or other selectors. By default, selectors are not writable and so can not alter the atoms/selections within it; only use it to provide its produced computed value. (get)

useRecoilValue – used to get/use the computed value from a selector.

Creating “n” amount of atoms

Sometimes we need an unknown amount of state atoms in an app. One way is to create a function that will returned a memorized atom based on an ID. We memorize it that way if it is the same ID we use the already made atom and return that instead of regenerating the atom.

For example:

Then we can use it in components like below:

 

React js – creating a parent component with variable amount of child components.

Problem: Had a “option” box I was creating where there could be any number of “tags” that can be added for that option.

Solution: Created a context local to the parent/children components only, kept a separate variable to manually track how many “groups” of options has been requested by the user. Used an array of objects and updated according to index which is sent in as a prop to each set of options. Example code

Here are the Parent and child components

Parent:

Child: