-
Notifications
You must be signed in to change notification settings - Fork 10
Lesson 1.2
JSX is an XML-based extension to JavaScript which just means it is a way to write JavaScipt code using XML. XML is a standard language similar to HTML that defines other languages. You can think of XML as a cousin to HTML.
React uses JSX elements to represent custom components. If you create a component named DisplayList, you can reuse that component in other components by using a JSX element named DisplayList. You can use any HTML5 element name when you write your React components and the result will be that React will output the HTML5 elements.
The rules of JSX are:
- All elements must be closed
- Attributes that are strings must have quotes around them
- HTML elements in JSX must be lowercase
- Attributes are written in camelCase
- Must return one parent element
- Reserved words such as class and for must be written as className and htmlFor
When you want to include JavaScript variables or a piece of JavaScript in your JSX, you need to use curly braces around it.
The job of a React component is to return an element.
const DisplayList = () => { // DisplayList component
const greeting = “Good morning”;
return (
<h2>{greeting}, Frank.</h2> // returns a React element
);
} A React component is a function or JavaScript class that optionally accepts data and returns a React element that describes some piece of the user interface. A React user interface is made up of a hierarchy of components that build up to a single component (called the root component ) that is rendered in the browser.
Once you import a component into another component, the imported component’s functionality will be included in your new component’s JSX using an element. You can include as many components inside another component, and there is no limit to as many levels of components a tree of components can have.
Once you import a component, you can use the element it defines as many times as you need to and each usage will create a new instance of the component with its own data and memory.
Components that are rendered inside other components are called children, and the component they’re rendered inside of is called their parent. A React UI of any complexity will have many components nested within other components and the parent/child terminology is how their relationships are described.
The HTML elements you define in your JSX of a component are actually built-in React components. React’s built-in HTML element components have the same name as elements from HTML5. Using them in your React app causes the equivalent HTML element to be rendered.
- Inside
/srcdirectory, create a new file calledTodoList.jsx - Open
/src/TodoList.jsx - Create a new functional React component (see below)
- Declare a function named
TodoList - Export
TodoListfunction as default module
- Declare a function named
- Add a multi-line return statement to your
TodoListfunction (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Move list JSX from
App.jsxtoTodoList.jsx(see below)- Open
/src/App.jsx - Cut (copy and remove) the entire list element (
<ul>) code - Open
/src/TodoList.jsx - Inside the multi-line return, paste the list element (
<ul>) code
- Open
- Move
todoListarray fromApp.jsxtoTodoList.jsx(see below)- Open
/src/App.jsx - Cut (copy and remove) the entire
todoListvariable declaration - Open
/src/TodoList.jsx - Paste the
todoListvariable
- Open
- Refactor
App.jsxto use newTodoListcomponent (see below)- Open
/src/App.jsx - Import
TodoList - Below the level-one heading, use the
TodoListcomponent
- Open
- Run your application and view in browser
- Verify that your Todo List still appears correctly
- Inside
/srcdirectory, create a new file calledAddTodoForm.jsx - Open
/src/AddTodoForm.jsx - Create a new functional React component (see below)
- Declare a function named
AddTodoForm - Export
AddTodoFormfunction as default module
- Declare a function named
- Add a multi-line return statement to your
AddTodoFormfunction (this is where we will insert JSX)- hint: use parenthesis for multi-line return
- Write JSX for "Add Todo" form (see below)
- Create a
<form>element - Inside the
<form>element, create a<label>element with text "Title" - Next, create a text
<input>element withid"todoTitle" - Add
htmlForattribute to<label>element with same value asidabove - Next, create a submit
<button>element with text "Add"
- Create a
- Use
AddTodoFormcomponent inApp.jsx(see below)- Open
/src/App.jsx - Import
AddTodoForm - Below the level-one heading, use the
AddTodoFormcomponent
- Open
- Run your application and view in browser
- Verify that "Add Todo" form appears below heading