Course Hero Logo

The component encapsulate html elements nested

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 18 - 22 out of 51 pages.

-The componentencapsulate HTMLelements.
Nested elements<Component>text</Component>What you put between the tags will bepassedtocomponent asprops.children.JSXmarkup is useful fordescribing UIstructures thathaveparent-child relationships.Childelements arecreated by nestingthem within another component: the parent..Using JavaScript expressions in JSX:JavaScript expressionsyntax looks like in JSX markup.JSXhas aspecial syntaxthatallowsyou toembedJavaScript expressions.Anything that is avalid JavaScript expression,including nested JSX, cangoin betweenthebraces:{ }.For properties and text, this is oftena variable nameorobject property.In this example, the !enabled expression computes aBoolean value.Rendered output looks like:
Mappingcollectionsto elementsThe best way to dynamically control JSX elements is tomapthem from acollection(Arrays, Objects). Let's look at an example of how this is done:Themap()functionlets you manipulate the items in an array/object byiteratingandaccessingindividual items.Tousethemap() function, attach it to anarray/objectyou want to iterate over.Thefirst collectionis an array calledarray, populated withstring values.Moving down to the JSX markup, you can see the call toarray.map(), whichreturnsanew array.Theobject collectionuses the same technique callObject.keys()and then mapthis array.Noneed torely on imperative logicto control theUI.Fragments of JSXFragmentsare a way togrouptogether chunks of markup (children)withouthaving toadd unnecessary structure to your page (extra node to DOM tree).React 16 introducesthe concept ofJSX fragments.For example, a common approach is to have a react componentreturncontent wrappedin a<div>element.Two versionsof a component:One uses awrapperelementUsesthe new fragmentfeatureLet's compare the two approaches now (WithoutFragments v.s WithFragments)WithoutFragmentsWithFragmentswrap sibling elements in <div>.Using unnecessary tagsEssence of this component is the <h1> and <p> tags<Fragment> element isused.DOM using browser dev tools.Children need to be rendered.<div> add to another level of structure:Less HTML renderedNo need to use tags such as <div>Week 10 End
Week 11What is componentstate?React components declare thestructure of UI elements using JSX.Stateis thedynamic part of a React component.It declares theinitial stateof a component, whichchangesover time.Whenever a React component needs tochange its state, the component willautomaticallyre-renderitself by callingrender().Creating and setting a component state:The JSX of this component depends ontwo state values:headingandcontent.The component isfirst renderedwith itsdefault state.

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 51 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Spring
Professor
N/A
Tags
JSON, Document Object Model, HTML element

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture