const foo = (
<div className="button">
<button type="submit"/>
</div>
)
// this gets transpiled to
const foo = (
React.createElement("div", {
className: "button"
children: [
React.createElement("button", { type: "submit" })
]
})
)
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
class Dropdown extends Component {
onToggle() {
this.setState({ visible: !this.state.visible })
}
render() {
return (
<div className="dropdown">
<button className="dropdown__button" onClick={this.onToggle}>
Menu
</button>
{this.state.visible &&
<div className="dropdown__content">
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/pricing">Pricing</Link>
</div>}
</div>
)
}
}
import React from 'react'
const Button = function Button({ type, ...props }) {
const className = type === 'white' ? 'button__white' : 'button__black'
return (
<button className={className} {...props}/>
)
}
const Input = observer(function Input({ field }) {
return (
<div className={classNames("form-group", {"has-error": field.error})}>
{field.error &&
<span className="form-group__error">{field.error}</span>}
<input className="form-control" {...field.bind()}/>
</div>
)
})
const StyledInput = function StyledInput(props) {
return (
<Input style={{ color: 'red' }} {...props}/>
)
}
// example use:
<StyledInput name="foo"/>
<StyledInput name="bar"/>
class UserFetch extends React.Component {
componentWillMount() {
fetch('/api/users').then((users) => this.setState({ users: users }))
}
render() {
const WrappedComponent = React.Children.only(this.props.children)
if(typeof this.state.users === 'undefined') {
return <div className="loading-spinner"/>
} else {
return React.cloneElement(WrappedComponent, { users: this.state.users })
}
}
}
// example:
const UserCount = ({ users }) => <div>User count: {users.length}</div>
const Users = () => (
<div>
<UserFetch><MyComponent/></UserFetch>
</div>
)
function withUserFetching(WrappedComponent) {
return class extends React.Component {
componentWillMount() {
fetch('/api/users').then((users) => this.setState({ users: users }))
}
render() {
if(typeof this.state.users === 'undefined') {
return <div className="loading-spinner"/>
} else {
return <WrappedComponent users={this.state.users}/>
}
}
}
}
// example:
const FetchingUserCount = withUserFetching(UserCount)
const Users = () => (
<div>
<FetchingUserCount/>
</div>
)
const Input = function Input({ inputRef, ...props }) {
return <input ref={inputRef} {...props}/>
}
import { observable, autorun } from 'mobx'
class Store {
@observable foo = 1
}
const store = new Store
autorun(() => console.log(store.foo))
store.foo = 2 // causes console.log to be called and 2 to be printed
store.foo = 3 // ditto, 3 is printed
import React from 'react'
import { observable } from 'mobx'
import { observer } from 'mobx-react'
class LikeButton extends React.Component {
@observable liked = false
render() {
return (
<div>
{this.liked ? "I like this" : "I don't like this"}
<button onClick={() => this.liked = !this.liked}/>
</div>
)
}
}
export default observer(LikeButton)
import { observable, action, computed } from 'mobx'
class UserStore {
@observable users = []
@observable current = null
fetch() {
fetch("/api/users").then(this.setUsers)
}
@action setUsers(users) {
this.users = users
}
@computed get currentUser() {
if(this.current !== null) {
return this.users.get(this.current)
}
}
}
const userStore = new UserStore
const projectStore = new ProjectStore
const stores = { userStore, projectStore }
const App = function App() {
return (
<Provider {..stores}>
<Header/>
<Routing/>
<Footer/>
</Provider>
)
}
const Header = function Header({ userStore }) {
return (
<header>
{userStore.currentUser
? <div>You are not logged in</div>
: <div>You are logged in as {userStore.currentUser.name}
}
</header>
)
}
export default inject('userStore')(observer(Header))
@inject('userStore')
@observer
class Footer extends React.Component {
render() {
const { userStore } = this.props
return (
<div>There are {userStore.users.length} users loaded</div>
)
}
}