|
| 1 | +--- |
| 2 | +title: Frequently Asked Questions |
| 3 | +description: Answers to problems that may be easily encountered by developers, as well as some interesting problems reported in GitHub issues. |
| 4 | +--- |
| 5 | + |
| 6 | +import { AppliesTo, RelatedIssues } from '@/components/AppliesTo' |
| 7 | + |
| 8 | +## Why is the component not updated when only the container width changes? |
| 9 | + |
| 10 | +<AppliesTo client:only="react" lang="en" /> |
| 11 | +<RelatedIssues client:only="react" lang="en" issues={[18]} /> |
| 12 | + |
| 13 | +Currently, the component has a built-in listener for the [Window: resize event](https://developer.mozilla.org/en-US/docs/Web/API/Window/resize_event) . When this event is triggered, the component recalculates its width and updates the displayed content. |
| 14 | + |
| 15 | +However, in some cases, such as when using [react-resizable-panels](https://github.com/bvaughn/react-resizable-panels) , only the container element’s width changes while the browser window width remains the same. In these scenarios, the component will not automatically adjust its size. |
| 16 | + |
| 17 | +### Reasons for not processing |
| 18 | + |
| 19 | +To avoid unnecessary performance overhead, the component does not include a built-in [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) for parent elements. Therefore, when the container size changes without a window resize event, developers need to decide when to trigger a re-render to ensure the component responds correctly to layout adjustments. |
| 20 | + |
| 21 | +### Recommended solution |
| 22 | + |
| 23 | +Recommend to use a `key` to trigger re-truncate, nodes will re-render when a `key` is updated, this is [React's ability](https://react.dev/reference/react/useState#resetting-state-with-a-key) . |
| 24 | + |
| 25 | +#### Trigger via callback API |
| 26 | + |
| 27 | +Set a variable and bind `key={refreshKey}` to the element that needs to respond to resize changes. |
| 28 | + |
| 29 | +```tsx |
| 30 | +const [refreshKey, setRefreshKey] = useState(Date.now()) |
| 31 | + |
| 32 | +// e.g. `<Truncate key={refreshKey} />` |
| 33 | +``` |
| 34 | + |
| 35 | +When the element triggers a resize change, update this value. |
| 36 | + |
| 37 | +```tsx |
| 38 | +const onResize = () => setRefreshKey(Date.now()) |
| 39 | + |
| 40 | +// e.g. `<Panel onResize={onResize} />` |
| 41 | +``` |
| 42 | + |
| 43 | +> Online demo: [CodeSandbox](https://codesandbox.io/p/sandbox/react-resizable-panels-forked-tjt38f?file=%2Fsrc%2FApp.js) |
| 44 | +
|
| 45 | +#### Trigger via dependency |
| 46 | + |
| 47 | +If there is a State dependency that can trigger React.useEffect, you can also implement it this way, declaring a common Hook. |
| 48 | + |
| 49 | +```ts |
| 50 | +// e.g. src/hooks/use-refresh-key.ts |
| 51 | +import React from 'react' |
| 52 | + |
| 53 | +export const useRefreshKey = (deps: unknown[]) => { |
| 54 | + const [refreshKey, setRefreshKey] = React.useState(Date.now()) |
| 55 | + |
| 56 | + React.useEffect(() => { |
| 57 | + requestAnimationFrame(() => { |
| 58 | + setRefreshKey(Date.now()) |
| 59 | + }) |
| 60 | + }, [deps]) |
| 61 | + |
| 62 | + return { |
| 63 | + refreshKey, |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +Treat the related state as a dependency. When the dependency changes, the value of `refreshKey` will be automatically updated. |
| 69 | + |
| 70 | +```tsx |
| 71 | +const [width, setWidth] = useState(DEFAULT_WIDTH_VALUE) |
| 72 | +const [lines, setLines] = useState(DEFAULT_LINES_VALUE) |
| 73 | + |
| 74 | +const { refreshKey } = useRefreshKey([width, lines]) |
| 75 | + |
| 76 | +// e.g. `<Truncate key={refreshKey} />` |
| 77 | +``` |
0 commit comments