James Sherry PRO
Web Development Tutor and Co-Founder of { The Jump } Digital School
import NoResult from './components/NoResult/NoResult.js';
import ListDisplay from './components/ListDisplay/ListDisplay.js';
function MyComponent(props) {
let subComponent = null;
if (props.items && props.items.length) {
subComponent = <ListDisplay items={props.items} />;
} else {
subComponent = <NoResult />;
}
return (
<div>
{subComponent}
</div>
);
}
function NumberList(props) {
const listItems = props.numbers.map((number, i) =>
<ListItem key={i} value={number} />
);
return (
<ul>
{listItems}
</ul>
);
}
function NumberList(props) {
return (
<ul>
{props.numbers.map((number, i) => <ListItem key={i} value={number} />)}
</ul>
);
}
OR
Using it:
Nesting Component:
function Item(props) {
return (
<ProductSurround>
<h1>{props.title}</h1>
</ProductSurround>
);
}
function ProductSurround(props) {
return (
<div style={{ border: '5px solid red', borderRadius: '15px'}}>
{props.children}
</div>
);
}
Sub-Component File
function Component(props) {
return (<button>{props.left}{props.text}{props.right}</button>);
}
import Icon from './components/icon/icon.js';
import Address from './components/address/address.js';
import SubComponent from './components/child/child.js';
function MyComponent(props) {
return (
<div>
<SubComponent
left={<Icon name="information" />}
right={<Icon name="go arrow" />}
text="Hello"
/>
</div>
);
}
Component File
import SubComponent from './components/child/child';
function MyComponent(props) {
return (
<div>
<SubComponent {...props} />
</div>
);
}
(using the cars API to demonstrate an AJAX GET)
Is a boilerplate
Component File
Main App File
export default function Item(props) {
return <p>{props.text}</p>;
}
import ChildComponent from './components/child/child'; // don't need .js
// if the URL is a directory with index.js in, that will be loaded
function App() {
return (
<div>
<h1>Heading</h1>
<ChildComponent text="Hello" />
</div>
);
}
export default App;
It comes in 3 parts:
By James Sherry
What we're trying to achieve when learning react