Skip to content

Instantly share code, notes, and snippets.

@vinkrish
Last active September 12, 2024 21:41
Show Gist options
  • Save vinkrish/bd50a45b5dcb1f6d12d4027b6b9187e9 to your computer and use it in GitHub Desktop.
Save vinkrish/bd50a45b5dcb1f6d12d4027b6b9187e9 to your computer and use it in GitHub Desktop.

npm install -g create-react-app

create-react-app name npm start

It puts fake HTML in javascript

To enable emmet: change VS Code Settings "emmet.includeLanguages": { "javascript": "javascriptreact", }

npm install enzyme enzyme-adapter-react-16 --save-dev

npm install react-router-dom

npm start -s

npm run lint

npm run lint -- --watch

For React-Native:

brew install yarn

Using Vite for React scaffolding npm create vite@latest
npm run dev

==========================================================================================

More Prop Syntaxes:

Beyond the various ways of setting and extracting props about which you learned in the previous lecture, there are even more ways of dealing with props.

But no worries, you'll see all these different features & syntaxes in action throughout the course!

Passing a Single Prop Object

If you got data that's already organized as a JavaScript object, you can pass that object as a single prop value instead of splitting it across multiple props.

I.e., instead of

<CoreConcept
  title={CORE_CONCEPTS[0].title}
  description={CORE_CONCEPTS[0].description}  
  image={CORE_CONCEPTS[0].image} />

or

<CoreConcept {...CORE_CONCEPTS[0]} />

you could also pass a single concept (or any name of your choice) prop to the CoreConcept component:

<CoreConcept concept={CORE_CONCEPTS[0]} />

In the CoreConcept component, you would then get that one single prop:

export default function CoreConcept({ concept }) {
  // Use concept.title, concept.description etc.
  // Or destructure the concept object: const { title, description, image } = concept;
}

It is entirely up to you which syntax & approach you prefer.

Grouping Received Props Into a Single Object:

You can also pass multiple props to a component and then, in the component function, group them into a single object via JavaScript's "Rest Property" syntax.

I.e., if a component is used like this:

<CoreConcept
  title={CORE_CONCEPTS[0].title}
  description={CORE_CONCEPTS[0].description}  
  image={CORE_CONCEPTS[0].image} />

You could group the received props into a single object like this:

export default function CoreConcept({ ...concept }) { 
  // ...concept groups multiple values into a single object
  // Use concept.title, concept.description etc.
  // Or destructure the concept object: const { title, description, image } = concept;
}

If that syntax is a bit confusing - worry not! You'll also see concrete examples for this syntax (and for why you might want to use it in certain situations) throughout the course!

Default Prop Values

Sometimes, you'll build components that may receive an optional prop. For example, a custom Button component may receive a type prop.

So the Button component should be usable either with a type being set:

<Button type="submit" caption="My Button" />

Or without it:

<Button caption="My Button" />

To make this component work, you might want to set a default value for the type prop - in case it's not passed.

This can easily be achieved since JavaScript supports default values when using object destructuring:

export default function Button({ caption, type = "submit" }) { 
  // caption has no default value, type has a default value of "submit"
}

==========================================================================================

Fragment

Fragment can be used as a wrapper to ensure that there's only one root JSX element whilst at the same time not rendering any DOM element.

You can use it like this:

import { Fragment } from 'react';
 
// ... other code ...
 
return (
  <Fragment>
    <h2>Welcome!</h2>
    <p>React is awesome!</p>
  </Fragment>
);

Most React projects (e.g., projects created with Vite or create-react-app) offer an even shorter form:

// no import needed
 
return (
  <>
    <h2>Welcome!</h2>
    <p>React is awesome!</p>
  </>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment