2017-06-03 44 views
0

我已經完成了一個歡迎框的代碼,並且可以通過單擊該按鈕來關閉它。這種歡迎只是頁面的一部分,現在,我想在此組件中添加cookie,以實現此歡迎框僅爲每個瀏覽器顯示一次。 我是cookie的新手,任何建議都會幫助我。如何在反應組件中設置Cookie

var Child = React.createClass({ 
 
    render: function() { 
 
    return (
 
     <div className="container"> 
 
     <p>Welcome to our website</p> 
 
     <button onClick={this.props.onClick}>close me</button> 
 
     </div> 
 
    ); 
 
    } 
 
}); 
 

 
var ShowHide = React.createClass({ 
 
    getInitialState: function() { 
 
    return { childVisible: true }; 
 
    }, 
 
    
 
    render: function() { 
 
    return(
 
     <div> 
 
     { 
 
      this.state.childVisible 
 
      ? <Child onClick={this.onClick} /> 
 
      : null 
 
     } 
 
     </div> 
 
    ) 
 
    }, 
 
    
 
    onClick: function() { 
 
    this.setState({childVisible: !this.state.childVisible}); 
 
    } 
 
}); 
 
    
 
React.render(<ShowHide />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react-dom.min.js"></script> 
 

 
<div id="container"> 
 
    <!-- This element's contents will be replaced with your component. --> 
 
</div>

回答