Parameter
Return
- value → 초기 값 / Promise 가 리턴한 값 / Promise 가 Reject 한 값
- state: “pending” | “fulfilled” | “rejected”
@observer
class SearchResults extends React.Component {
@observable.ref searchResults
componentDidUpdate(nextProps) {
if (nextProps.query !== this.props.query)
this.searchResults = fromPromise(
window.fetch("/search?q=" + nextProps.query),
// by passing, we won't render a pending state if we had a successful search query before
// rather, we will keep showing the previous search results, until the new promise resolves (or rejects)
this.searchResults
)
}
render() {
return this.searchResults.case({
pending: (staleValue) => {
return staleValue || "searching" // <- value might set to previous results while the promise is still pending
},
fulfilled: (value) => {
return value // the fresh results
},
rejected: (error) => {
return "Oops: " + error
}
})
}
}
// Observable promises can be created immediately in a certain state using
// `fromPromise.reject(reason)` or `fromPromise.resolve(value?)`.
// The main advantage of `fromPromise.resolve(value)` over `fromPromise(Promise.resolve(value))` is that the first _synchronously_ starts in the desired state.
// It is possible to directly create a promise using a resolve, reject function:
// `fromPromise((resolve, reject) => setTimeout(() => resolve(true), 1000))`
Parameter
Return
- ObservableArray
const source = observable([1, 2, 3])
moveItem(source, 0, 1)
console.log(source.map(x => x)) // [2, 1, 3]
Parameter
const userProfile = lazyObservable(
sink => fetch("/myprofile").then(profile => sink(profile))
)
// use the userProfile in a React component:
const Profile = observer(({ userProfile }) =>
userProfile.current() === undefined
? <div>Loading user profile...</div>
: <div>{userProfile.current().displayName}</div>
)
// triggers refresh the userProfile
userProfile.refresh()
Parameter
- subscriber
- unsubscriber: IDisposer
- initialValue: T
function createObservableUser(dbUserRecord) {
let currentSubscription;
return fromResource(
(sink) => {
// sink the current state
sink(dbUserRecord.fields)
// subscribe to the record, invoke the sink callback whenever new data arrives
currentSubscription = dbUserRecord.onUpdated(() => {
sink(dbUserRecord.fields)
})
},
() => {
// the user observable is not in use at the moment, unsubscribe (for now)
dbUserRecord.unsubscribe(currentSubscription)
}
)
}
// usage:
const myUserObservable = createObservableUser(myDatabaseConnector.query("name = 'Michel'"))
// use the observable in autorun
autorun(() => {
// printed everytime the database updates its records
console.log(myUserObservable.current().displayName)
})
// ... or a component
const userComponent = observer(({ user }) =>
<div>{user.current().displayName}</div>
)
Parameter
- expression
- fireImmediately: boolean
const user = observable({
firstName: "C.S",
lastName: "Lewis"
})
Rx.Observable
.from(mobxUtils.toStream(() => user.firstname + user.lastName))
.scan(nameChanges => nameChanges + 1, 0)
.subscribe(nameChanges => console.log("Changed name ", nameChanges, "times"))