gpt4 book ai didi

reactjs - 无法将 useState Hook 值设置为相反的 bool 值

转载 作者:行者123 更新时间:2023-12-03 13:41:59 26 4
gpt4 key购买 nike

我正在创建一个 DatePicker 并使用 useState Hook 来管理它的可见性。在 div 单击时,我添加了更改值的事件监听器,但它没有按我的预期工作。它仅在第一次起作用,因此初始值更改为 true,但在第二次和第三次单击时,该值保持为 true,并且 DatePicker 在单击时保持可见。

这是日期选择器

import React, { useState } from 'react';
import OutsideClickHandler from 'react-outside-click-handler';
import { renderInfo, getWeeksForMonth } from './utils';
import {
renderMonthAndYear,
handleBack,
handleNext,
weekdays,
} from '../../utils';

const DatePicker = ({
isOpen,
setIsOpen,
selected,
setSelected,
dayClick,
dayClass,
}) => {
const startDay = new Date().setHours(0, 0, 0, 0);

const [current, setCurrent] = useState(new Date(startDay));

const weeks = getWeeksForMonth(current.getMonth(), current.getFullYear());

function handleClick(date) {
if (date > startDay) {
setSelected(date);
setCurrent(date);
setIsOpen(false);

if (dayClick) {
dayClick(date);
}
}
}

return (
<div className="DatePicker-container">
<div
tabIndex="0"
role="button"
className="DatePicker-info"
onKeyPress={e => {
if (e.which === 13) {
setIsOpen(!isOpen);
}
}}
onClick={e => {
setIsOpen(!isOpen);
}}
>
{renderInfo(selected)}
</div>
{isOpen && (
<OutsideClickHandler onOutsideClick={() => setIsOpen(false)}>
<div className="DatePicker">
<div className="DatePicker__header">
<span
role="button"
onClick={() => handleBack(current, setCurrent)}
className="triangle triangle--left"
/>

<span className="DatePicker__title">
{renderMonthAndYear(current)}
</span>

<span
role="button"
onClick={() => handleNext(current, setCurrent)}
className="triangle triangle--right"
/>
</div>

<div className="DatePicker__weekdays">
{weekdays.map(weekday => (
<div
key={weekday}
className="DatePicker__weekday"
>
{weekday}
</div>
))}
</div>
{weeks.map((week, index) => (
<div
role="row"
key={index}
className="DatePicker__week"
>
{week.map((date, index) =>
date ? (
<div
role="cell"
key={index}
onClick={() => handleClick(date)}
className={dayClass(date)}
>
{date.getDate()}
</div>
) : (
<div
key={index}
className="DatePicker__day--empty"
/>
),
)}
</div>
))}
</div>
</OutsideClickHandler>
)}
</div>
);
};

export default DatePicker;

DateRangePicker 使用两个。

import React, { useState } from 'react';
import DatePicker from './DatePicker';
import DatePickerContext from './DatePickerContext';
import './DatePicker.scss';

const DateRangePicker = () => {
const startDay = new Date().setHours(0, 0, 0, 0);

const [isOpen, setIsOpen] = useState(false);
const [isSecondOpen, setIsSecondOpen] = useState(false);
const [selected, setSelected] = useState(new Date(startDay));
const [secondSelected, setSecondSelected] = useState(new Date(startDay));

function dayClass(date) {
if (
selected.getTime() === date.getTime() ||
(date >= selected && date <= secondSelected)
) {
return 'DatePicker__day DatePicker__day--selected';
}
if (date < startDay || date < selected) {
return 'DatePicker__day DatePicker__day--disabled';
}
return 'DatePicker__day';
}

function dayClick(date) {
setSecondSelected(date);
setIsSecondOpen(true);
}

return (
<DatePickerContext.Provider>
<div className="DatePicker-wrapper">
<DatePicker
key={1}
isOpen={isOpen}
setIsOpen={setIsOpen}
selected={selected}
setSelected={setSelected}
dayClick={dayClick}
dayClass={dayClass}
/>
<DatePicker
key={2}
isOpen={isSecondOpen}
setIsOpen={setIsSecondOpen}
selected={secondSelected}
setSelected={setSecondSelected}
dayClass={dayClass}
/>
</div>
</DatePickerContext.Provider>
);
};

export default DateRangePicker;

最佳答案

a) 使用 functional updates 是一个很好的做法当下一个状态依赖于前一个(==当前)状态时,确保使用正确的“当前”值:

setIsOpen(currentIsOpen => !currentIsOpen)
<小时/>

b) 当为同一事件执行的多个处理程序更新下一个状态时,很难推断出下一个状态。以下 2 个处理程序可能会在同一点击上执行(第一个 div 位于“外部”):

<div ... onClick={e => setIsOpen(!isOpen)}>
<OutsideClickHandler onOutsideClick={() => setIsOpen(false)}>

如果 onOutsideClick 首先执行,然后 React 使用 isOpen=false 重新渲染,然后 onClick 第二个执行,它会设置 isOpen=true 正如你所观察到的 - 我不知道重新渲染是如何发生的,但也许 OutsideClickHandler 正在做一些邪恶的事情,或者你的代码比问题中的更复杂́\_(ツ)_/́

仅强制执行 1 个事件处理程序:

<OutsideClickHandler onOutsideClick={(e) => {
e.stopPropagation();
setIsOpen(false);
}}>

关于reactjs - 无法将 useState Hook 值设置为相反的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231752/

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