<info 340/>

React Components

Joel Ross
Autumn 2023

View of the Day

  • Review: React & JSX

  • Review: React Components (code demo)

  • Component Props (code demo)

Updating Lecture Code

# switch to starter branch to get new starter code
git checkout starter

# download new starter code
git pull

# switch back to main branch for coding
git checkout main

# merge in new starter code (use default msg)
git merge starter --no-edit

# code and enjoy!

Get the starter code from the starter branch, but do all of your work on main.

Create React App

create-react-app is a command line application that generates scaffolding ("starter code") for a React website.

# EITHER create a new react app
npx create-react-app app-name --use-npm

# OR install dependencies for existing project
cd path/to/project
npm install

# run the server
npm start

Runs a script that starts a development server which will:

  • Automatically transpile React code into pure JavaScript
  • Combine (bundle) different modules into a single file
  • Show build and syntax errors in the console, including style warnings
  • Automatically reload the page (replaces live-server)!

JSX

Use {} to include JavaScript expressions in the JSX. These expressions will be evaluated and inserted into the element's "HTML".

//Can include JavaScript expressions in React elements
const message = "Hello world!";
const element = <h1>{message}</h1>;


//Can include arbitrary expressions
const element = (
  <p>
    A leap year has {(365 + 1) * 24 * 60} minutes!
  </p>
);

//Can use inline expressions in attributes
const imgUrl = 'path/to/my_picture.png';
const pic = <img src={imgUrl} alt="A picture" />;

replace with expression (value)

React elements
must be closed

Bootstrap in React

To use Bootstrap (styling) in React, install the library as a dependency and then import the css file.

Install the dependency

# you must be in the React project folder
cd path/to/my-react-app

# install dependency
npm install bootstrap
/* in index.js */
import 'bootstrap/dist/css/bootstrap.css';
import 'my-style.css' // import your stylesheet afterwards

Import the stylesheet

React Components

We define components as functions that return the DOM elements to be rendered

//declare a function to define a component
function HelloMessage(props) {

  
  //this function returns the elements (JSX)
  //that make up the component
  return (
     <h1>Hello World!</h1>
  );
}

//"call" function to create a new element value!
const msgElem = <HelloMessage />;

//show the content in the web page (inside #root)
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(msgElem)

what is rendered when
component is shown

our own HTML tags!

Capitalize!

Composing Components

function HelloItem(props) {
   return <li>Hello World!</li>;
}

function GoodbyeItem(props) {
   return <li>See ya later!</li>;
}

function MessageList(props) {
   return (
      <ul>
         <HelloItem /> {/* A HelloItem component */}
       	 <GoodbyeItem /> {/* A GoodbyeItem component */}
      </ul>
   );
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(msgElem)

Components can render other components ("call" functions to create new elements), and mix those with regular DOM elements

Properties (props)

//Passing a prop called `message` with value "Hello property"
const messageA = <MessageItem message="Hello property!" />;

//A component can accept multiple props
//This component takes in a `name` property as well as 
//a `descriptor` property
const userInfo = <UserInfo name="Ethel" descriptor="Aardvark" />;

//Passing a value as a prop using an inline expression
const secret = "Shave and a haircut";
const messageB = <MessageItem message={secret} />;

We specify attributes for a component (called "props") when we instantiate a component by specifying the XML attributes (key-value).

Props are the "input parameters" into a component!

Properties (props)

function MessageItem(props) {
   const message = props.message; //access the prop



    //can use prop for logic or processing
   const messageUpper = message.toUpperCase(); 

   return <li>{messageUpper}</li>; //render based on prop
}

const msgElem = <MessageItem message="Be quiet" />

Inside the Component function definition, all the passed in props are passed in as a single argument object (conventionally called props). This object is the collection of all of the attributes/arguments.

ALL props stored in this object

Properties (props)

//pass in TWO props
const msgElem = <MessageItem message="Make some noise!" isLoud={true} />
  
//receives ONE argument
function MessageItem(props) {
   const message = props.message; //access the `message` prop
   const isLoud = props.isLoud; //access the `isLoud` prop

   let displayMsg = message;
   if(isLoud){
     displayMsg = message.toUpperCase();
   } 

   return <li>{displayMsg}</li>; //render
}

Component functions only take a single argument. ALL props are collected into the single props argument.

(Name the argument props, with an s at the end).

Properties (props)

//Pass an array as a prop!
const array = [1,2,3,4,5];
const suitcaseElem = <Suitcase luggageCombo={array} />;


//Pass a function as a prop (like a callback)!
function sayHello() { 
  console.log('Hello world!');
}
const greetingElem = <Greeting callback={sayHello} />;

Importantly, props can be any kind of variable! This includes arrays, objects and functions

Props and Composition

function MessageList(props) {
  //msgComponents will be an array of components!
  const msgComponents = props.messages.map((msgStr) => {
    const elem = <MessageItem message={msgStr} key={msgStr} />; //pass prop down!
    return elem
  }

  return (
    <ul>
      {/* An array of components renders as siblings */}
      {msgComponents} 
    </ul>
  );
}

const messagesArray = ["Hello world", "No borders", "Go huskies!"];

ReactDOM.createRoot(document.getElementById('root'))
   .render(<MessageList messages={messagesArray} />)

Props will often need to be "passed down" to child components. A common pattern is to map an array of prop values to an array of children components to render!

unique "id" for the element

Conditional Rendering

You can use control logic (if statements) to specify whether or not a component should be rendered.

function ConditionalPanel(props) {
  //assign element to show to variable
  let thingToRender = null; //null element will not render
  if(conditionOne){ //based on props or state
    thingToRender = <OptionA />
  } else if(conditionTwo) {
    thingToRender = <OptionB />
  } else if(conditionThree) {
    return null; //show nothing!
  }
    
  //keep return statement as simple as possible!
  return (<div>{thingToRender}</div>);
}
function ConditionPanel(props) {
  //can use inline expressions via shortcutting. Not recommended
  return (
    <div>
      {conditionOne == true && <OptionA />}
    </div>
  )
}

Action Items!

Action Items!

  • Review Ch 15: React

  • Read Ch 16: Interactive React

  • Problem Set 07 due week from Friday

    • You can do problem-a now!

    • Get started early; project draft 2 is due
      right after

Next time: More React -- adding interactivity

info340au23-react-components

By Joel Ross

info340au23-react-components

  • 194