var foo = {
bar: 123
};
var {bar} = foo;
// Again
var {bar} = {
bar: 123
};
var foo = function () { return 123 };
var foo = () => 123;
https://upload.wikimedia.org/wikipedia/commons/4/47/River_terrapin.jpg
const Body = ({body})=> <div>{body}</div>
const Headline = ({title}) =>
<div style={{fontSize:"24px"}}>{title}</div>;
const News = ({title,body}) =>
<div>
<Headline title={title}/>
<Body body={body}/>
</div>;
const Headline = (props:{title:string}) =>
<div style={{fontSize:"24px"}}>{props.title}</div>;
const Headline = (props:{title:string}) =>
<div style={{fontSize:"24px"}}>{props.title}</div>;
// Render to DOM
ReactDOM.renderComponent(
<Headline title="Hello World")/>,
document.getElementById("app")
);
// Render on server
const str = ReactDOMServer.renderToString(
<Headline title="Hello World")/>
);
interface Props {
text: string;
}
interface State {
}
export class FooFancy extends React.Component<Props, State>{
render() {
return <div>{this.props.text}</div>;
}
}
interface Props {
text: string;
}
interface State {
count: number;
}
export class FooFancy extends React.Component<Props, State>{
constructor(props:Props){
super(props);
this.state = {
count: 0
}
setInterval(()=>{ this.setState({count: this.state.count + 1}) }, 1);
}
render() {
return <div>{this.props.text} - {this.state.count}</div>;
}
}
interface Props {
text: string;
}
interface State {
count: number;
}
export class FooFancy extends React.Component<Props, State>{
constructor(props:Props){
super(props);
this.state = {
count: 0
}
}
interval:number;
componentDidMount(){
this.interval =
setInterval(() => this.setState({ count: this.state.count + 1 }));
}
componentWillUnmount(){
clearInterval(this.interval);
}
render() {
return <div>{this.props.text} - {this.state.count}</div>;
}
}
constructor
XHRs
propsToState
this. state == state
dispose()
// whole line == "Statement"
var foo = 123;
// A sourcefile is just
// a bunch of *statements* ;)
var foo =
/*this is an expression */ 123;
// Warning: Invalid JavaScript!
var foo = {
bar : if (true) { 123 }
}
// \o/
var foo = {
bar : true && 123
}
render() {
return <div>
{true?"Asdf":undefined}
{
<div>asdf</div>
}
</div>;
}
<div className="foo"/>
<label htmlFor="foo"/>
These match the JavaScript access for these in browser DOM e.g.`node.className`
var foo = document.createElement('div');
foo.
var content = (
<Nav>
{/* We need more Person */}
<Person
// Name only set if logged in
name={window.isLoggedIn ? window.name : ''}
/>
</Nav>
);
return (
<Nav>
{/* We need more Person */}
<Person
// Name only set if logged in
name={window.isLoggedIn ? window.name : ''}
/>
</Nav>
);
return <Fancy ref="root"/>;
return React.createElement(Fancy,
{"ref": "root"}
);
return <div ref="root"/>;
return React.createElement("div",
{"ref": "root"}
);
div
Div
render() {
return <input
type="text"
value={this.state.message}
onChange={(event)=>this.setState({message:event.target.value})} />;
}
class FooFancy extends React.Component{
componentDidMount() {
this.refs["root"].style.color = 'red';
}
render() {
return <div ref="root">Hello World</div>;
}
}
class BarFancy extends React.Component{
sayYouLoveMe(){
alert('I love you!');
}
render(){
return <div>I can say I love you</div>;
}
}
class FooFancy extends React.Component{
refs: {
[key:string]:any;
root: BarFancy;
}
componentDidMount() {
this.refs.root.sayYouLoveMe();
}
render() {
return <BarFancy ref="root"/>;
}
}
class BarFancy extends React.Component {
render() {
return <div> Raise the bar! </div>;
}
}
class FooFancy extends React.Component {
render() {
return <div>
{ [1, 2, 3].map((i)=> <BarFancy key={i}/>) }
</div>;
}
}