Category Archives: Javascript

Vuex Notes

Example Vuex with some comment notes

In main.js we add the store via:

In a component that uses something in the store:

Mutations are for non-async updates to the Vuex store. Actions can use async code. Generally, should actions should always be used instead of Mutators.

If using Mutators.. the update to the store can happen directly. i.e. this.$store.commit('mutatorMethodThatUpdatesStore');

Actions use dispatch instead: this.$store.dispatch('mutatorMethodThatUpdatesStore');

3 useful methods from Vuex are:

mapGetters

mapMutations

mapActions

mapState – mapState should be used instead of creating “dummy” getters. Dummy getters are ones that simply just return the store’s state value.

Getters should be more used for computed type values or for things such as a filter etc.

For each of the 4 above..All have either an array of strings of names of state/getter/mutation/action or can pass in an object with key/value pairs if needing a different name. For example:

Notes for Udemy Course “The Complete React Web Developer Course (with Redux)”

Notes for Udemy Course “The Complete React Web Developer Course (with Redux)” – https://www.udemy.com/react-2nd-edition/

React uses jsx and ES6 which means it requires Babel ( requires node ). Two scripts are required to work with React in the browser. React and ReactDOM. Below is a sample React component that has  toggle visibility functionality.

 

If components are nested, data as props can only be sent down – not up. So, on the parent component a method can be passed to the child component as a prop in order to send the information back to the parent.

Stateless functional components

Another type of component  that can be reduced to a single function call because they do not have any state to handle of their own. Mostly used for “presentation” type components. They can use props but aren’t accessible through *this* .

This type of component is “faster” as it has less overhead as it doesn’t handle state.

Small tricks for conditional rendering

For the arrow function, if you are return just an object you can use the following syntax:

In React, methods to update a parent component are passed down to child components via props.

Lifecycle methods

Methods are available to hook in to when a class based component is added/removed/updated in React. Common ones to use are componentDidMount() for fetching initial data, and componentDidUpdate(prevProps, prevState) for saving updated changes back to the db.

When using componentDidUpdate, make sure the prevState and current state are not equal ( !==) so that a save to db/localStorage is not called that is not needed.

 

Localstorage Notes

When within a method that was attached to a onClick event ( or any other similar method ), you can reference the field/value by the following type of example:

Children props ( slots )

Dynamic content that be added between a components tag can be added to be placed within the layout of the components it’s self. For example:

 

Simple form value/onChange practice:

Can create a single handleChange method to handle all form values and updates that are used in state.

Now we can use same method for all form fields:

 

Misc React Notes

In JSX, all normal html attributes such as onclick and onchange MUST be camel case.. i.e.e onClick and onChange

All components must start with a capital letter.

JSX conditional rendering :

ternary operator is accepted i.e. true ? ‘show this if true’ : ‘ value if false’

if/else can not be used in {} jsx since it is a statement and not an expression.

&& AND Operator