import React, {PropTypes} from 'react'
import resolveRefType from './resolveRefType'
import {resolver as previewResolver} from 'part:@sanity/base/preview'
export default class SanityPreview extends React.PureComponent {

  static propTypes = {
    value: PropTypes.object,
    type: PropTypes.object.isRequired
  };

  state = {
    loading: true,
    materialized: null,
    refType: null
  }

  resolveRefType(value, type) {
    resolveRefType(value, type).then(refType => {
      this.setState({
        refType
      })
    })
  }

  componentDidMount() {
    const {type, value} = this.props
    this.resolveRefType(value, type)
  }

  componentWillReceiveProps(nextProps) {
    if (this.props.type !== nextProps.type || this.props.value !== nextProps.value) {
      this.resolveRefType(nextProps.value, nextProps.type)
    }
  }

  render() {
    const {refType} = this.state

    if (!refType) {
      return null
    }

    const Preview = previewResolver(refType)
    return (
      <Preview {...this.props} type={refType} />
    )
  }
}

@@ -394,41 +394,246 @@
 %0A%0A  
-resolveRefType(value, type) %7B%0A   
+componentDidMount() %7B%0A    const %7Btype, value%7D = this.props%0A    this.subscribeRefType(value, type)%0A  %7D%0A%0A  componentWillUnmount() %7B%0A    this.unsubscribe()%0A  %7D%0A%0A  subscribeRefType(value, type) %7B%0A    this.unsubscribe()%0A    this.subscription =
  res
@@ -660,13 +660,25 @@
 ype)
-.then
+%0A      .subscribe
 (ref
@@ -687,16 +687,18 @@
 pe =%3E %7B%0A
+  
       th
@@ -714,34 +714,20 @@
 te(%7B
-%0A        refType%0A      
+refType
 %7D)%0A
+  
     
@@ -740,103 +740,127 @@
 %0A%0A  
-componentDidMount() %7B%0A    const %7Btype, value%7D = this.props%0A    this.resolveRefType(value, type)
+unsubscribe() %7B%0A    if (this.subscription) %7B%0A      this.subscription.unsubscribe()%0A      this.subscription = null%0A    %7D
 %0A  %7D
@@ -999,22 +999,24 @@
    this.
-resolv
+subscrib
 eRefType

Fix wrong resolving of referenced type