【发布时间】:2017-11-04 14:53:20
【问题描述】:
我已经完成了一个欢迎框的代码,可以通过单击按钮将其关闭。这个欢迎只是页面的一部分,现在,我想在这个组件中添加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>
【问题讨论】:
标签: javascript reactjs cookies