$( ".item" ).on( "click", doSomething );
But sometimes this gets messy...
Most basic React Component
class HelloWorld extends React.Component {
render(){
return (
Hello World
);
}
}
// Example usage: <HelloWorld />
because this is the alternative
return React.createElement(
'p', {className: 'greeting'}, "Hello World"
);
React.createClass({displayName: "ExampleComponent",
render: function () {
return (
React.createElement("div", {className: "example"},
React.createElement("h1", null, "Example Component"),
React.createElement("ul", null, React.createElement("li", null, "One item"),
React.createElement("li", null, "Another item"))
)
);
}
});
className
becauseclass
is a reserved word in JS.
class HelloWorld extends React.Component {
render() {
return Hello World
;
}
}
...
return (
Hello
World
);
class Profile extends React.Component {
render() {
return (
{ this.props.displayName }
);
}
}
Passing data from one component to another
They pass additional information to an element.
tagline
...
return (
<Header tagline="Thank You, Come Again!" />
)
Access data in child component via this.props.tagline
class Header extends React.Component {
...
render(){
return <h2>{this.props.tagline}</h2>
}
}
$( ".item" ).on( "click", doSomething );
<button onClick={activateLasers}>
Activate Lasers
</button>
Values that get passed from parent to child components.
State is an object. Any component can have state.
constructor
method.
class App extends React.Component {
constructor() {
super();
// get initial state
this.state = {
foo: {}
};
}
updateFoo(foo) {
this.setState({
foo: foo
});
}
...
}
const DumbComponent = (props) => {
// no need for render() method
return (
{props.text}
);
}
# View Helper's Signature
react_component(component_class_name, props={}, html_options={})
# Our Usage
react_component("SM.ACARootComponent", @operation.to_hash)