react.dev icon indicating copy to clipboard operation
react.dev copied to clipboard

Documentation error in the Render Props - Needs Interpolation

Open hariprasad9899 opened this issue 3 years ago • 1 comments
trafficstars

Hi Team,

I am trying to give a small correction in the documentation code of React under the topic Render Props. This isn't such a big issue, but will definitely help newbies and must be addressed.

Under the Render Props concept in React documentation, in one of the examples, where a class component of Cat is created which returns an image, which is positioned absolutely. The position of that image is received through render props.

The received position coordinates are passed to the absolute positioned image. Now the problem is instead of using interpolation and adding units into it, the position coordinates values are directly given in the style object. Surprisingly the Img still moves respectively to the mouse pointer.

But I don't think giving the values without units is the right thing. So, the below code should be changed from

From: render() { const mouse = this.props.mouse; return ( <img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} /> ); } To: render(){ const mouse = this.props.mouse; return ( <img src= './imgs/cat.jpg' style={{ left:${mouse.x}px, top: ${mouse.y}px}} /> ) }

If my understanding is wrong, please correct me.

Much thanks!

Regards, Hari prasad

hariprasad9899 avatar Sep 12 '22 14:09 hariprasad9899

In JSX you can use the number value as the value of the top or left style attribute. React takes care of translating it into the correct CSS style properties. So, this is correct.

<img src="/cat.jpg" style={{ position: 'absolute', left: mouse.x, top: mouse.y }} /> );

zqran avatar Sep 13 '22 01:09 zqran