You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the ResponsiveLine component, I send x values that are Date objects. After the chart is drawn, some of my Date objects have been changed. Specifically, the first and last data item in the first series have been changed from midnight UTC to midnight in the local time zone.
I tried to make a codesandbox.io but could not get the site to work for me.
I created this reproducible example, as simple as possible. Here's the App.tsx:
importReact,{useEffect}from'react';importReactDOMfrom'react-dom/client';import{ResponsiveLine}from'@nivo/line';constApp: React.FC=()=>{constchartData=[{id: 'id',data: [{x: newDate('2023-01-01T00:00:00Z'),y: 120},{x: newDate('2023-01-02T00:00:00Z'),y: 150},{x: newDate('2023-01-03T00:00:00Z'),y: 130},{x: newDate('2023-01-04T00:00:00Z'),y: 180},],},];// Helper function for consistent logging of Date object's internal stateconstlogDateStates=(prefix: string)=>{chartData[0].data.forEach((point,index)=>{// Check if the UTC time components are all zeros (i.e., exactly UTC midnight)constisMidnightUTC=point.x.getUTCHours()===0&&point.x.getUTCMinutes()===0&&point.x.getUTCSeconds()===0&&point.x.getUTCMilliseconds()===0;// Define the style for the console logconstcolorStyle=isMidnightUTC ? '' : 'color: red; font-weight: bold;';console.log(`${prefix} Point ${index}:`);// Apply the style to the ISO String and getTime() logsconsole.log(`%c ISO String (internal): ${point.x.toISOString()}`,colorStyle);console.log(`%c getTime() (milliseconds UTC epoch): ${point.x.getTime()}`,colorStyle);});};// Log immediately when the component function is called (before render)console.log('--- LOGS: BEFORE RENDER ---');logDateStates('BEFORE RENDER');// Log after the component has initially rendereduseEffect(()=>{console.log('--- LOGS: AFTER INITIAL RENDER ---');logDateStates('AFTER INITIAL RENDER');},[]);// Empty dependency array ensures this runs only once after mount// Log on button click (to check state after Nivo has potentially mutated)consthandleLogChartData=()=>{console.log('--- LOGS: ON BUTTON CLICK ---');logDateStates('ON BUTTON CLICK');};return(<divstyle={{padding: '20px',maxWidth: '900px',margin: '0 auto'}}><divstyle={{height: '400px',width: '100%'}}><ResponsiveLinedata={chartData}xScale={{type: 'time',format: 'native',precision: 'day',}}/></div><buttononClick={handleLogChartData}style={{marginTop: '20px',padding: '10px 20px',backgroundColor: '#007bff',color: 'white',border: 'none',borderRadius: '5px',cursor: 'pointer',fontSize: '16px'}}>LogchartDataStateNow</button>{/* Explanatory text for the bug report */}<pstyle={{marginTop: '20px',fontSize: '0.9em',lineHeight: '1.4em'}}>Somethingabout<code>ResponsiveLine</code>ismutatingthedatesinthe<code>data</code>prop.<br/>ThisistheshortestreproducibleversionIcouldcreate.ThedatesarecreatedasmidnightUTC.<br/>Thedatesandtimesremainokayoncomponentloadandarefineafterthefirst'render',butifyouclickthe"Log chartData State Now"buttonafterdrawingiscomplete,youwillseethatthefirstandthelastdataitemintheserieshavechangedtheirinternalUTCtimes.Theyshouldnotchange.Adding<code>xScale.useUTC: true</code>doesnotpreventthis.</p></div>);};// Render the App component into the DOMconstroot=ReactDOM.createRoot(document.getElementById('root')asHTMLElement);root.render(<React.StrictMode><App/></React.StrictMode>);exportdefaultApp;
btw, thank you for the detailed report, even if you didn't provide something I could immediately run, it was straightforward to reproduce (however, it's best to mention the version you're using).
Using the ResponsiveLine component, I send x values that are Date objects. After the chart is drawn, some of my Date objects have been changed. Specifically, the first and last data item in the first series have been changed from midnight UTC to midnight in the local time zone.
I tried to make a codesandbox.io but could not get the site to work for me.
I created this reproducible example, as simple as possible. Here's the App.tsx:
Desktop
OS: Ubuntu 24.04.02 LTS
Browser: Firefox 139.0 and Chromium 136.0.7103.113
The text was updated successfully, but these errors were encountered: