Sunday, April 12, 2020

what's jsx

A: html inside javascript

how to render component to dom

A: ReactDOM.render(<App />, document.getElementById('app'));

no public src folder created after using create-react-app

A: Try with these steps :
  1. npm rm -g create-react-app
  2. npm install -g create-react-app
  3. npx create-react-app my-app

Tuesday, December 31, 2019

load component dynamically

checkoutCancelledHandler = () => {
this.props.history.goBack();
};
checkoutContinuedHandler = () => {
this.props.history.replace('/checkout/contact-data');
};
render() {
return (
<div>
<CheckoutSummary
ingredients={this.state.ingredients}
checkoutCancelled={this.checkoutCancelledHandler}
checkoutContinued={this.checkoutContinuedHandler}
/>
<Route
path={this.props.match.path + '/contact-data'}
render={props => (
<ContactData
ingredients={this.state.ingredients}
price={this.state.price}
{...props}
/>
)}
/>
</div>
);
}


======when using redux, no need to pass props

checkoutCancelledHandler = () => {
this.props.history.goBack();
};
checkoutContinuedHandler = () => {
this.props.history.replace('/checkout/contact-data');
};
render() {
return (
<div>
<CheckoutSummary
ingredients={this.props.ings}
checkoutCancelled={this.checkoutCancelledHandler}
checkoutContinued={this.checkoutContinuedHandler}
/>
<Route
path={this.props.match.path + '/contact-data'}
component={ContactData}
/>
</div>
);
}


Friday, December 27, 2019

Immutable Update Patterns

Updating Nested Objects

The key to updating nested data is that every level of nesting must be copied and updated appropriately. This is often a difficult concept for those learning Redux, and there are some specific problems that frequently occur when trying to update nested objects. These lead to accidental direct mutation, and should be avoided.
Common Mistake #1: New variables that point to the same objects
Defining a new variable does not create a new actual object - it only creates another reference to the same object. An example of this error would be:
This function does correctly return a shallow copy of the top-level state object, but because the nestedState variable was still pointing at the existing object, the state was directly mutated.
Common Mistake #2: Only making a shallow copy of one level
Another common version of this error looks like this:
Doing a shallow copy of the top level is not sufficient - the nestedState object should be copied as well.
Correct Approach: Copying All Levels of Nested Data
Unfortunately, the process of correctly applying immutable updates to deeply nested state can easily become verbose and hard to read. Here's what an example of updating state.first.second[someId].fourth might look like:
Obviously, each layer of nesting makes this harder to read, and gives more chances to make mistakes. This is one of several reasons why you are encouraged to keep your state flattened, and compose reducers as much as possible.

Inserting and Removing Items in Arrays

Normally, a Javascript array's contents are modified using mutative functions like pushunshift, and splice. Since we don't want to mutate state directly in reducers, those should normally be avoided. Because of that, you might see "insert" or "remove" behavior written like this:
However, remember that the key is that the original in-memory reference is not modified. As long as we make a copy first, we can safely mutate the copy. Note that this is true for both arrays and objects, but nested values still must be updated using the same rules.
This means that we could also write the insert and remove functions like this:
The remove function could also be implemented as:

Updating an Item in an Array

Updating one item in an array can be accomplished by using Array.map, returning a new value for the item we want to update, and returning the existing values for all other items:

Immutable Update Utility Libraries

Because writing immutable update code can become tedious, there are a number of utility libraries that try to abstract out the process. These libraries vary in APIs and usage, but all try to provide a shorter and more succinct way of writing these updates. Some, like dot-prop-immutable, take string paths for commands:
Others, like immutability-helper (a fork of the now-deprecated React Immutability Helpers addon), use nested values and helper functions:
They can provide a useful alternative to writing manual immutable update logic.

Tuesday, December 17, 2019

Passing params via Query Params

on sending page

const queryParams = [];
for (let i in this.state.ingredients) {
queryParams.push(
encodeURIComponent(i) +
'=' +
encodeURIComponent(this.state.ingredients[i])
);
}
const queryString = queryParams.join('&');
this.props.history.push({
pathname: '/checkout',
search: '?' + queryString
});

on receiving page

componentDidMount() {
const query = new URLSearchParams(this.props.location.search);
const ingredients = {};
for (let param of query.entries()) {
ingredients[param[0]] = +param[1];
}
this.setState({ ingredients: ingredients });
}
checkoutCancelledHandler = () => {
this.props.history.goBack();
};

Sunday, September 22, 2019

CSS Course - 3: A bit deeper into CSS


  • The Box Model
  • Height & Width
  • The "display" Property
  • Properties worth to remember
  • Pseudo classes & elements
CSS Box Model

Margin
   border
      padding
         content


  • The Box Model
  • Height & Width
  • The "display" Property
  • Properties worth to remember
  • Pseudo classes & elements
CSS Box Model

Margin
   border
      padding
         content


Margin Collapsing - Bigger Margin “wins”

In general: Use either margin-top or margin-bottom

Shorthand Properties - Combine values of multiple properties in a single property (the shorthand property)

Example: 

border-width: 2px;
border-style: dashed | solid
border-color: orange

Shorthand property:
border: 2px dashed orange
margin: 5px 10px 5px 10px;  // top right bottom left
margin: 5px 10px; //top & bottom;  left & right
margin: 10px; //all sides

Height & Width properties

Block element default to 100% width

box-sizing: content-box (default) |border-box

Display Property

Display: none|block|inline|inline-block

Pseudo Classes & Pseudo Elements

Pseudo class - Defines the style of a special state of an element, eg: hover, active

Pseudo element - Defines the style of a specific part of an element, eg: ::after, ::selection, ::first-line

Check MDN standard pseudo-classes


Properties Worth to Remember

color:
background-color:
display:
padding:
border:
margin:
width:
height: