gpt4 book ai didi

javascript - 在react.js中为组件设置cookie

转载 作者:行者123 更新时间:2023-12-03 04:10:32 29 4
gpt4 key购买 nike

我读过这篇文章How can I do to set cookie in the react code?关于在reactjs代码中设置cookie,但是,我无法使用任何库,我只能使用javascrip。现在,我想要实现的是在第一次访问时显示一个欢迎框。我所做和测试的是: 1.欢迎框,它是jsx中的组件编码; 2.js中的cookie编码。这两个子部分都经过测试工作正常,但是,当我尝试将这两个部分组合成一个组件时,它不起作用。

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>

最佳答案

除了 cookie,您还可以使用 localStorage 。这是一个基本示例。一旦安装了主组件,它就会在存储中设置一个项目,并在 render() 方法中对该项目使用react。

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 .

在其他组件中,您只需检查 localStorage.getItem('hasVisitedBefore') 并基于此渲染内容。

关于javascript - 在react.js中为组件设置cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44353193/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com