Goo*_*mja 12 reactjs server-side-rendering next.js react-dates
我正在使用 Next.js 和react-dates构建一个应用程序。
我有两个组件DateRangePicker组件和DayPickerRangeController组件。
当窗口的宽度大于1180px时,我想渲染DateRangePicker,如果尺寸小于这个,我想渲染DayPickerRangeController。
这是代码:
windowSize > 1180 ?
<DateRangePicker
startDatePlaceholderText="Start"
startDate={startDate}
startDateId="startDate"
onDatesChange={handleOnDateChange}
endDate={endDate}
endDateId="endDate"
focusedInput={focus}
transitionDuration={0}
onFocusChange={(focusedInput) => {
if (!focusedInput) {
setFocus("startDate")
} else {
setFocus(focusedInput)
}
}}
/> :
<DayPickerRangeController
isOutsideRange={day => isInclusivelyBeforeDay(day, moment().add(-1, 'days'))}
startDate={startDate}
onDatesChange={handleOnDateChange}
endDate={endDate}
focusedInput={focus}
onFocusChange={(focusedInput) => {
if (!focusedInput) {
setFocus("startDate")
} else {
setFocus(focusedInput)
}
}}
/>
}
Run Code Online (Sandbox Code Playgroud)
我通常使用反应钩与窗口对象检测窗口屏幕宽度像这
但是我发现ssr的时候这种方式是不可用的,因为ssr渲染没有window对象。
无论ssr如何,是否有另一种方法可以安全地获得窗口大小?
Dar*_*ert 34
您可以通过添加以下代码来避免在 ssr 中调用检测函数:
// make sure your function is being called in client side only
if (typeof window !== 'undefined') {
// detect window screen width function
}
Run Code Online (Sandbox Code Playgroud)
来自您链接的完整示例:
import { useState, useEffect } from 'react';
// Usage
function App() {
const size = useWindowSize();
return (
<div>
{size.width}px / {size.height}px
</div>
);
}
// Hook
function useWindowSize() {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined,
});
useEffect(() => {
// only execute all the code below in client side
if (typeof window !== 'undefined') {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Add event listener
window.addEventListener("resize", handleResize);
// Call handler right away so state gets updated with initial window size
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener("resize", handleResize);
}
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}
Run Code Online (Sandbox Code Playgroud)
useEffect(()=> {
window.addEventListener('resize', ()=> {
console.log(window.innerHeight, window.innerWidth)
})
}, [])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14514 次 |
| 最近记录: |