【问题标题】:How to set up cookie in react component如何在 React 组件中设置 cookie
【发布时间】: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


    【解决方案1】:

    您可以访问以下链接。你需要导入 react-cookie

    How can I do to set cookie in the react code?

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 2018-05-30
      • 2017-11-05
      • 1970-01-01
      • 2017-09-07
      • 2017-03-06
      • 2019-10-22
      • 2016-09-18
      • 1970-01-01
      相关资源
      最近更新 更多