Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Cookie在触发页面加载时消失,但在其他页面上不消失_Javascript_Reactjs_Cookies_React Hooks - Fatal编程技术网

Javascript Cookie在触发页面加载时消失,但在其他页面上不消失

Javascript Cookie在触发页面加载时消失,但在其他页面上不消失,javascript,reactjs,cookies,react-hooks,Javascript,Reactjs,Cookies,React Hooks,我正在使用React引导模式,用户可以选择他是用户或代理,网站内容将基于此进行更新 通过按钮选择用户类型: <Button onClick={handleUser}>USER</Button> <Button onClick={handleAgent}>AGENT</Button> const [selectedValue, setSelectedValue] = useState(''); const userRequirement = s

我正在使用React引导模式,用户可以选择他是用户代理,网站内容将基于此进行更新

通过按钮选择用户类型:

 <Button onClick={handleUser}>USER</Button>
 <Button onClick={handleAgent}>AGENT</Button>
const [selectedValue, setSelectedValue] = useState('');
const userRequirement = selectedValue === '' ? 'user' : 'agent';

const handleUser = () => {
    setSelectedValue('user');
    setShow(false);
};

const handleAgent = () => {
    setSelectedValue('agent');
    setShow(false);
};
const cookies = new Cookies();
cookies.set('cookie', selectedValue);
该模式在1sec用户在网站上处于活动状态后触发,当然,cookie是根据用户类型设置的

useEffect(() => {
    if (cookies === cookies.set) {
        setShow(false);
    } else {
        setTimeout(() => {
            setShow(true);
        }, 1000);
    }
}, []);
我的cookies设置:

 <Button onClick={handleUser}>USER</Button>
 <Button onClick={handleAgent}>AGENT</Button>
const [selectedValue, setSelectedValue] = useState('');
const userRequirement = selectedValue === '' ? 'user' : 'agent';

const handleUser = () => {
    setSelectedValue('user');
    setShow(false);
};

const handleAgent = () => {
    setSelectedValue('agent');
    setShow(false);
};
const cookies = new Cookies();
cookies.set('cookie', selectedValue);
问题是,当我选择用户类型(例如代理时,cookie设置为具有代理值,当我单击另一个页面时,cookie仍然作为代理存在,但问题是当我返回索引页面(触发模式)时,cookie突然消失,因此,再次触发模态

我在
useffect
中尝试实现的预期行为:

选择用户类型-将其存储在cookie中,除非cookie过期或被删除,否则不要再次触发模式

谁知道我做错了什么