Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll in React

Tags:

scroll

reactjs

I Have a React div tag below which actually holds json tree. The problem is overflow-x for horizontal scroll is not working. I am posting the code and error below.Is there any way for horizontal scroll using css in react.Vertical scroll is automatically working if mere overflow: 'scroll' is given.

const divStyle={
        overflow-y: 'scroll',
        border:'1px solid red',
        width:'500px',
        float: 'left',
        height:'500px',
        position:'relative'
      };

<div style={divStyle}>
                    <Droppable
                        types={['yolo']}
                        style={droppableStyle}
                        onDrop={this.onDrop.bind(this)}
                        onDragEnter={this.onDragEnter.bind(this)}
                        onDragLeave={this.onDragLeave.bind(this)}>
                        <div style={{textAlign:'left', lineHeight:'100px' ,overflow:'scroll'}}>{this.state.dropped}</div>
                    </Droppable>
                </div>

strong text tag(parent) overflow-x if given it gives error as below.

./src/Drag.js Syntax error: Unexpected token, expected , (38:16)

36 | render() { 37 | const divStyle={

38 | overflow-y: 'scroll', | ^ 39 | border:'1px solid red', 40 | width:'500px', 41 | float: 'left',

like image 928
Akash Sateesh Avatar asked Jun 13 '17 16:06

Akash Sateesh


People also ask

How do you make a scroll in React?

Scroll Methodsvar Scroll = require('react-scroll'); var Element = Scroll. Element; var scroller = Scroll. scroller; <Element name="myScrollToElement"></Element> // Somewhere else, even another file scroller.

How do you scroll to view in React?

Scroll to an Element With the React Refs (References): The most simple way is to use ref to store the reference of the element that you want to scroll to. And call myRef. current. scrollIntoView() to scroll it into the view.

How do I scroll smoothly in React JS?

Create components folder in the src. Write below code, const Scroll = () => { return null; } export default Scroll; Import scrollbar from smooth-scrollbar.

How do you control scroll in React?

U need to set window. scrollTo(0, 0) when u click on link so it would start on top of page.


2 Answers

Try this:

style={{overflowX : 'auto',fontSize: '14px'}} for inline css in reactjs.

It works fine.

All styles with a dash is converted to camel case in reactjs and remove dash.

like image 28
Bhautik Dobariya Avatar answered Oct 17 '22 11:10

Bhautik Dobariya


Remember that divStyle is an object and object keys, just like other identifier names, such as function names, etc, cannot have dashes/hyphens unless the key is written as a string literal.

However, react recognizes only the CamelCase version, so use that instead:

const divStyle={
  overflowY: 'scroll',
  border:'1px solid red',
  width:'500px',
  float: 'left',
  height:'500px',
  position:'relative'
};

Here's a snippet from the official Reactjs docs:

The style attribute accepts a JavaScript object with camelCased properties rather than a CSS string. This is consistent with the DOM style JavaScript property, is more efficient, and prevents XSS security holes. For example:

const divStyle = {
  color: 'blue',
  backgroundImage: 'url(' + imgUrl + ')',
};

function HelloWorldComponent() {
  return <div style={divStyle}>Hello World!</div>;
}
like image 89
Chris Avatar answered Oct 17 '22 10:10

Chris