[英]Set up cookie for the component in react.js
I have read this article How can I do to set cookie in the react code? 我已经阅读了这篇文章如何在React代码中设置cookie? about setting up cookie in reactjs code, however, I cannot use any library, what I can use is just the javascrip.
关于在reactjs代码中设置cookie,但是,我不能使用任何库,只能使用javascrip。 Now, what I want to achieve is to show up a welcome box for the first time visit.
现在,我想要实现的是在第一次访问时显示一个欢迎框。 What I have done and tested are: 1.the welcome box, it is a component coding in jsx;
我已经完成并测试的是:1.欢迎框,它是jsx中的组件编码; 2. the cookie coding in js.
2. js中的cookie编码。 Both sub-part are tested working, however, when I tried to combine these two part into a single component, it is not working.
这两个子部分都经过测试工作,但是,当我尝试将这两个部分组合为一个组件时,它就无法工作。
var Child = React.createClass({ render: function() { return ( <div className="container"> <p>Welcome to our website</p> <button onClick={this.props.onClick}>Click me to close</button> </div> ); } }); var ShowHide = React.createClass({ createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; }, readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return ""; }, eraseCookie(name) { createCookie(name,"",-1); }, firstVisit() { var isFirstVisit; var result = readCookie('firstVisit'); if (result == "") { createCookie('firstVisit', 'true', 365); isFirstVisit = true; return isFirstVisit; } else { isFirstVisit = false; return isFirstVisit; } }, getInitialState: function () { if(firstVisit()) { return { childVisible: true }; } else { return { childVisible: true }; } }, onClick: function() { this.setState({childVisible: !this.state.childVisible}); }, render: function() { return( <div> { this.state.childVisible ? <Child onClick={this.onClick} /> : null } </div> ) }, }); React.render(<ShowHide />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Alternatively to cookies you could use localStorage
. localStorage
cookie,您可以使用localStorage
。 Here's a basic example of that. 这是一个基本的例子。 It sets an item in the storage once the main component is mounted and reacts to that item in the
render()
method. 一旦安装了主要组件,它将在存储中设置一个项目,并在
render()
方法中对该项目做出反应。
componentDidMount() {
// Get item from localStorage and save it to a constant.
const hasVisitedBefore = localStorage.getItem('hasVisitedBefore');
// Check if the user has visited the site (component) before.
if (!hasVisitedBefore) {
// If not, set the component state (or dispatch an action if you need)
// Also set the localStorage so it's globally accessible.
this.setState({ hasVisitedBefore: false });
localStorage.setItem('hasVisitedBefore', true);
}
}
// Based on the state set in `componentDidMount()`, show or hide the welcome modal.
render() {
return (
<div>
{this.state.hasVisitedBefore
? 'Welcome back!'
: 'Welcome for the first time!'}
</div>
);
}
Working demo on JSFiddle . JSFiddle上的工作演示 。
In other components you could then just check for localStorage.getItem('hasVisitedBefore')
and render things based on that. 然后,在其他组件中,您只需检查
localStorage.getItem('hasVisitedBefore')
并根据该内容进行渲染即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.