2016-05-16 17 views
0

lightboxのsrcを使用しようとしています。このコードは最新バージョンの反応では機能していないようです。 LightboxTriggerコンポーネントの親コンポーネントから子の小道具を設定できません。親から子に小道具を設定できません

反応例を使用した実施例0.12。 example

var LightboxModal = React.createClass({ 

    whiteContentStyles: { 
     position: 'fixed', 
     top: '25%', 
     left: '30%', 
     right: '30%', 
     backgroundColor: '#fff', 
     color: '#7F7F7F', 
     padding: '20px', 
     border: '2px solid #ccc', 
     borderRadius: '20px', 
     boxShadow: '0 1px 5px #333', 
     zIndex:'101' 
    }, 

    blackOverlayStyles: { 
     background: 'black', 
     opacity: '.5', 
     position: 'fixed', 
     top: '0px', 
     bottom: '0px', 
     left: '0px', 
     right: '0px', 
     zIndex: '100' 
    }, 

    closeTagStyles: { 
     float: 'right', 
     marginTop: '-30px', 
     marginRight: '-30px', 
     cursor: 'pointer', 
     color: '#fff', 
     border: '1px solid #AEAEAE', 
     borderRadius: '30px', 
     background: '#605F61', 
     fontSize: '31px', 
     fontWeight: 'bold', 
     display: 'inline-block', 
     lineHeight: '0px', 
     padding: '11px 3px', 
     textDecoration: 'none' 
    }, 

    componentDidMount: function(){ 
     document.addEventListener("keydown", function (e) { 
      if ((this.props.display) && (e.keyCode === 27)){ 
       this.props.closeLightbox(); 
      } 
     }.bind(this)); 
    }, 

    render: function(){ 
     for (var j in this.props){ 
      if (j !== 'children'){ 
       this.props.children.props[j] = this.props[j]; 
      } 
     } 

     if (this.props.display){ 
      return (
       <div> 
        <div style={this.blackOverlayStyles} onClick={this.props.closeLightbox} /> 
        <div style={this.whiteContentStyles}> 
         <a style={this.closeTagStyles} onClick={this.props.closeLightbox}>&times;</a> 
         {this.props.children} 
        </div> 
       </div> 
      ); 
     } else { 
      return (<div></div>); 
     } 
    } 
}); 


var LightboxTrigger = React.createClass({ 
    render: function(){ 

     this.props.children.props.onClick = this.props.openLightbox; 
     for (var j in this.props){ 
      if (j !== 'children'){ 
       this.props.children.props[j] = this.props[j]; 
      } 
     } 
     return this.props.children; 
    } 
}); 


var Lightbox = React.createClass({ 

    getInitialState: function(){ 
     return { display: false }; 
    }, 

    componentWillMount: function(){ 
     if (this.props.data) 
      this.setState(this.props.data); 
    }, 

    openLightbox: function(){ 
     this.setState({display: true}); 
    }, 

    closeLightbox: function(){ 
     this.setState({display: false}); 
    }, 

    setLightboxState: function(obj){ 
     this.setState(obj); 
    }, 

    render: function(){ 
     var childrenWithProps = React.Children.map(this.props.children, function(child) { 
      var childProps = { 
       openLightbox: this.openLightbox, 
       closeLightbox: this.closeLightbox, 
       setLightboxState: this.setLightboxState 
      }; 
      console.log(childProps) 
      for (var j in this.state){ 
       childProps[j] = this.state[j]; 
      } 

      var childWithProps = React.cloneElement(child, childProps); 

      return childWithProps; 
     }, this); 

     return (
      <div> 
       {childrenWithProps} 
      </div> 
     ); 
    } 
}); 

これをdomでレンダリングしています。

<Lightbox> 
    <LightboxTrigger> 
     <a href="#">Click to open</a> 
    </LightboxTrigger> 
    <LightboxModal> 
     <div> 
      <h1>This is the basic usage!</h1> 
      <p>Good luck :D</p> 
     </div> 
    </LightboxModal> 
</Lightbox> 
+0

について読むことができますか? – hansn

+0

@hansn何もありません。ちょうど子供の財産を設定しないでください! – Kaushik

+0

どのプロパティを設定しようとしていますか、どこに設定しようとしていますか? – hansn

答えて

1

リアクション14以上を使用している場合、プロップオブジェクトは現在フリーズしており、変更することはできません。代わりにReact.cloneElementを使用できます。

あなたがエラーか何かを得ているこの変更here、およびReact.CloneElement here

関連する問題