const hi = <h1>Hello, JSX! 1 + 1 == {1 + 1}. today is {new Date()}</h1>JSX也是表达式
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
const img = <img src={user.photo} alt="this is a photo." />注意,不要使用引号把{}包起来。
const element = (
<div>
<h1 className="hi">Hello!</h1>
<h2>Good to see you here.</h2>
</div>
);
const title = response.potentiallyMaliciousInput;
// This is safe:
const element = <h1>{title}</h1>;
const element = <div dangerouslySetInnerHTML={{__html: '<h1>Dangerous!</h>'}} />
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);
const element = React.createElement(
'h1',
{className: 'greeting'},
'Hello, world!'
);
// Note: this structure is simplified
const element = {
type: 'h1',
props: {
className: 'greeting',
children: 'Hello, world'
}
};
<img src="x.jpg" alt="a pic" />会被翻译成:
React.createElement( 'img',
{src: 'x.jpg', alt: 'a pic'}, null)
import React from 'React';
import CustomButton from './CustomButton';
function WarningButton() {
// return React.createElement(CustomButton, {color: 'red'}, null);
return <CustomButton color="red" />;
}
import React from 'react';
// Wrong! This is a component and should have been capitalized:
function hello(props) {
// Correct! This use of <div> is legitimate because div is a valid HTML tag:
return <div>Hello {props.toWhat}</div>;
}
function HelloWorld() {
// Wrong! React thinks <hello /> is an HTML tag because it's not capitalized:
return <hello toWhat="World" />;
// 可以这样修复
//const Hello = hello;
//return <Hello toWhat="World" />;
}
import React from 'react';
// Correct! This is a component and should be capitalized:
function Hello(props) {
// Correct! This use of <div> is legitimate because div is a valid HTML tag:
return <div>Hello {props.toWhat}</div>;
}
function HelloWorld() {
// Correct! React knows <Hello /> is a component because it's capitalized.
return <Hello toWhat="World" />;
}
import React from 'react';
const MyComponents = {
DatePicker: function DatePicker(props) {
return <div>Imagine a {props.color} datepicker here.</div>;
}
}
function BlueDatePicker() {
return <MyComponents.DatePicker color="blue" />;
}
import React from 'react';
import { PhotoStory, VideoStory } from './stories';
const components = {
photo: PhotoStory,
video: VideoStory
};
function Story(props) {
// Wrong! JSX type can't be an expression.
return <components[props.storyType] story={props.story} />;
}
function Story(props) {
// Correct! JSX type can be a capitalized variable.
const SpecificStory = components[props.storyType];
return <SpecificStory story={props.story} />;
}
<MyComponent foo={1 + 2 + 3 + 4} />字符串
<MyComponent message="hello world" />
<MyComponent message={'hello world'} />
<MyTextBox autocomplete />
<MyTextBox autocomplete={true} />
function App1(props) {
return <Greeting firstName={props.firstName} lastName={props.lastName} />;
}
function App2(props) {
return <Greeting {...props} />;
}
<App1 props={firstName: 'Ben', lastName: 'Hector'} />
<App2 props={firstName: 'Ben', lastName: 'Hector'} />
<div>Hello World</div>
<div>
Hello World
</div>
<div>
Hello
World
</div>
<div>
Hello World
</div>
<MyContainer>
Here is a list:
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
<ComponentA>sth...</ComponentA>
<ComponentB />
</MyContainer>
<MyComponent>foo</MyComponent>
<MyComponent>{'foo'}</MyComponent>
<MyComponent>{1 + 2 * 3}</MyComponent>
function Item(props) {
return <li>{props.message}</li>;
}
function TodoList() {
const todos = ['finish doc', 'submit pr', 'nag dan to review'];
return (
<ul>
{todos.map((message) => <Item key={message} message={message} />)}
</ul>
);
}
// Calls the children callback numTimes to produce a repeated component
function Repeat(props) {
let items = ;
for (let i = 0; i < props.numTimes; i++) {
items.push(props.children(i));
}
return <div>{items}</div>;
}
function ListOfTenThings() {
return (
<Repeat numTimes={10}>
{(index) => <div key={index}>This is item {index} in the list</div>}
</Repeat>
);
}
<div />
<div></div>
<div>{false}</div>
<div>{null}</div>
<div>{undefined}</div>
<div>{true}</div>
<div>
{showHeader && <Header />}
<Content />
</div>
<div>
{props.messages.length &&
<MessageList messages={props.messages} />
}
</div>
<div>
{props.messages.length > 0 &&
<MessageList messages={props.messages} />
}
</div>
value is {String(true)}
value is {'' + null}
value is {`${undefined}`}
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。