Set textarea scroll from ReactJS statechange scrollTop in reactjsHow do I remove a property from a JavaScript object?Setting “checked” for a checkbox with jQuery?How to Check if element is visible after scrolling?Set a default parameter value for a JavaScript functionScroll to the top of the page using JavaScript/jQuery?Get selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?jQuery scroll to elementHow do I return the response from an asynchronous call?Correct modification of state arrays in ReactJS

Why do people keep referring to Leia as Princess Leia, even after the destruction of Alderaan?

Cops: The Hidden OEIS Substring

How to disable wifi in Raspberry Pi 4

Does the Dispel Magic spell work on the Mirror Image spell?

Professor falsely accusing me of cheating in a class he does not teach, two months after end of the class. What precautions should I take?

Why isn't pressure filtration popular compared to vacuum filtration?

Comparing two limsup's

Cubic programming and beyond?

How can an advanced civilization forget how to manufacture its technology?

If your plane is out-of-control, why does military training instruct releasing the joystick to neutralize controls?

What is this welding tool I found in my attic?

Supporting developers who insist on using their pet language

The monorail explodes before I can get on it

Why does Hellboy file down his horns?

<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?

How does a Potion of Poison work?

Why do players in the past play much longer tournaments than today's top players?

Book where the stars go black due to aliens stopping human observation collapsing quantum possibilities

What do the horizontal lines in a P-V phase diagram mean?

For a hashing function like MD5, how similar can two plaintext strings be and still generate the same hash?

Managing and organizing the massively increased number of classes after switching to SOLID?

Extract an attribute value from XML

Does throwing a penny at a train stop the train?

Referring to different instances of the same character in time travel



Set textarea scroll from ReactJS state


change scrollTop in reactjsHow do I remove a property from a JavaScript object?Setting “checked” for a checkbox with jQuery?How to Check if element is visible after scrolling?Set a default parameter value for a JavaScript functionScroll to the top of the page using JavaScript/jQuery?Get selected text from a drop-down list (select box) using jQueryHow do I remove a particular element from an array in JavaScript?jQuery scroll to elementHow do I return the response from an asynchronous call?Correct modification of state arrays in ReactJS






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I'm trying to control the scroll position of a textarea using a React component's state, but it doesn't move when I call setState().



Here's an example where clicking on the link should scroll to the top of the textarea, but it doesn't move. Scrolling with the scroll bar can log the positions, and the field moves. Clicking on the link writes a message to the console, but the scroll position doesn't move. I based this approach on the controlled components described in the ReactJS documentation.






class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





I can control the scroll position by using the ref and setting its scrollTop, but I want to control it through the component state or the component properties. (Eventually, I want to synchronize the scrolling of two textareas.) I also tried using the react-scroll-sync library, but it doesn't work well with table cells.



Here's an example that controls the position using the ref and its scrollTop:






class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>












share|improve this question



















  • 2





    I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

    – Tommy May
    Mar 26 at 3:58


















0















I'm trying to control the scroll position of a textarea using a React component's state, but it doesn't move when I call setState().



Here's an example where clicking on the link should scroll to the top of the textarea, but it doesn't move. Scrolling with the scroll bar can log the positions, and the field moves. Clicking on the link writes a message to the console, but the scroll position doesn't move. I based this approach on the controlled components described in the ReactJS documentation.






class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





I can control the scroll position by using the ref and setting its scrollTop, but I want to control it through the component state or the component properties. (Eventually, I want to synchronize the scrolling of two textareas.) I also tried using the react-scroll-sync library, but it doesn't work well with table cells.



Here's an example that controls the position using the ref and its scrollTop:






class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>












share|improve this question



















  • 2





    I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

    – Tommy May
    Mar 26 at 3:58














0












0








0








I'm trying to control the scroll position of a textarea using a React component's state, but it doesn't move when I call setState().



Here's an example where clicking on the link should scroll to the top of the textarea, but it doesn't move. Scrolling with the scroll bar can log the positions, and the field moves. Clicking on the link writes a message to the console, but the scroll position doesn't move. I based this approach on the controlled components described in the ReactJS documentation.






class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





I can control the scroll position by using the ref and setting its scrollTop, but I want to control it through the component state or the component properties. (Eventually, I want to synchronize the scrolling of two textareas.) I also tried using the react-scroll-sync library, but it doesn't work well with table cells.



Here's an example that controls the position using the ref and its scrollTop:






class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>












share|improve this question
















I'm trying to control the scroll position of a textarea using a React component's state, but it doesn't move when I call setState().



Here's an example where clicking on the link should scroll to the top of the textarea, but it doesn't move. Scrolling with the scroll bar can log the positions, and the field moves. Clicking on the link writes a message to the console, but the scroll position doesn't move. I based this approach on the controlled components described in the ReactJS documentation.






class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





I can control the scroll position by using the ref and setting its scrollTop, but I want to control it through the component state or the component properties. (Eventually, I want to synchronize the scrolling of two textareas.) I also tried using the react-scroll-sync library, but it doesn't work well with table cells.



Here's an example that controls the position using the ref and its scrollTop:






class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>








class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
scrollTop=this.state.scrollTop
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





class App extends React.Component 
constructor(props)
super(props);
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.refs.content.scrollTop;
console.log(scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.refs.content.scrollTop = 0;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref="content"
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>






javascript reactjs scrollbar






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 4:45







Don Kirkby

















asked Mar 26 at 3:40









Don KirkbyDon Kirkby

29.6k13 gold badges138 silver badges212 bronze badges




29.6k13 gold badges138 silver badges212 bronze badges







  • 2





    I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

    – Tommy May
    Mar 26 at 3:58













  • 2





    I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

    – Tommy May
    Mar 26 at 3:58








2




2





I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

– Tommy May
Mar 26 at 3:58






I do not believe that scrollTop is a valid HTML attribute. It seems like it is a DOM property with no corresponding HTML attribute, which is why it works when you use it as a property on a ref, but not when you use it to change the HTML attribute. Looking at the list of valid HTML attributes I do not see it, developer.mozilla.org/en-US/docs/Web/HTML/Attributes. I know a lot of times attributes have 1:1 mappings to properties but this does not seem to be the case with scrollTop.

– Tommy May
Mar 26 at 3:58













1 Answer
1






active

oldest

votes


















1














Tommy May explained in a comment that scrollTop isn't an HTML attribute, so I can't link it directly to the component state or properties. However, I found that the componentDidUpdate() method can be used to handle changes to the state or properties.



Here's an updated example that uses componentDidUpdate():






class App extends React.Component 
constructor(props)
super(props);
this.state = scrollTop: 0;
this.content = React.createRef();
this.handleScroll = this.handleScroll.bind(this);
this.handleClick = this.handleClick.bind(this);


handleScroll()
let scrollTop = this.content.current.scrollTop;
console.log(scrollTop);
this.setState(scrollTop: scrollTop);


handleClick(e)
e.preventDefault();
console.log('Clicked.');
this.setState(scrollTop: 0);


componentDidUpdate()
this.content.current.scrollTop = this.state.scrollTop;


render()
let text = 'annbnncnndnnennfnng';
return <p><textarea
ref=this.content
value=text
rows="10"
cols="30"
onScroll=this.handleScroll/>
<a href="#" onClick=this.handleClick>
Scroll to top
</a></p>;



ReactDOM.render(
<App/>,
document.getElementById('root')
);

.as-console-wrapper max-height: 10% !important; 

<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>





This isn't particularly useful in this example, but it is useful when you want to use properties instead of local state, as in this answer I posted for my original goal of synchronized scrolling.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349521%2fset-textarea-scroll-from-reactjs-state%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Tommy May explained in a comment that scrollTop isn't an HTML attribute, so I can't link it directly to the component state or properties. However, I found that the componentDidUpdate() method can be used to handle changes to the state or properties.



    Here's an updated example that uses componentDidUpdate():






    class App extends React.Component 
    constructor(props)
    super(props);
    this.state = scrollTop: 0;
    this.content = React.createRef();
    this.handleScroll = this.handleScroll.bind(this);
    this.handleClick = this.handleClick.bind(this);


    handleScroll()
    let scrollTop = this.content.current.scrollTop;
    console.log(scrollTop);
    this.setState(scrollTop: scrollTop);


    handleClick(e)
    e.preventDefault();
    console.log('Clicked.');
    this.setState(scrollTop: 0);


    componentDidUpdate()
    this.content.current.scrollTop = this.state.scrollTop;


    render()
    let text = 'annbnncnndnnennfnng';
    return <p><textarea
    ref=this.content
    value=text
    rows="10"
    cols="30"
    onScroll=this.handleScroll/>
    <a href="#" onClick=this.handleClick>
    Scroll to top
    </a></p>;



    ReactDOM.render(
    <App/>,
    document.getElementById('root')
    );

    .as-console-wrapper max-height: 10% !important; 

    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
    <div id="root"></div>





    This isn't particularly useful in this example, but it is useful when you want to use properties instead of local state, as in this answer I posted for my original goal of synchronized scrolling.






    share|improve this answer





























      1














      Tommy May explained in a comment that scrollTop isn't an HTML attribute, so I can't link it directly to the component state or properties. However, I found that the componentDidUpdate() method can be used to handle changes to the state or properties.



      Here's an updated example that uses componentDidUpdate():






      class App extends React.Component 
      constructor(props)
      super(props);
      this.state = scrollTop: 0;
      this.content = React.createRef();
      this.handleScroll = this.handleScroll.bind(this);
      this.handleClick = this.handleClick.bind(this);


      handleScroll()
      let scrollTop = this.content.current.scrollTop;
      console.log(scrollTop);
      this.setState(scrollTop: scrollTop);


      handleClick(e)
      e.preventDefault();
      console.log('Clicked.');
      this.setState(scrollTop: 0);


      componentDidUpdate()
      this.content.current.scrollTop = this.state.scrollTop;


      render()
      let text = 'annbnncnndnnennfnng';
      return <p><textarea
      ref=this.content
      value=text
      rows="10"
      cols="30"
      onScroll=this.handleScroll/>
      <a href="#" onClick=this.handleClick>
      Scroll to top
      </a></p>;



      ReactDOM.render(
      <App/>,
      document.getElementById('root')
      );

      .as-console-wrapper max-height: 10% !important; 

      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
      <div id="root"></div>





      This isn't particularly useful in this example, but it is useful when you want to use properties instead of local state, as in this answer I posted for my original goal of synchronized scrolling.






      share|improve this answer



























        1












        1








        1







        Tommy May explained in a comment that scrollTop isn't an HTML attribute, so I can't link it directly to the component state or properties. However, I found that the componentDidUpdate() method can be used to handle changes to the state or properties.



        Here's an updated example that uses componentDidUpdate():






        class App extends React.Component 
        constructor(props)
        super(props);
        this.state = scrollTop: 0;
        this.content = React.createRef();
        this.handleScroll = this.handleScroll.bind(this);
        this.handleClick = this.handleClick.bind(this);


        handleScroll()
        let scrollTop = this.content.current.scrollTop;
        console.log(scrollTop);
        this.setState(scrollTop: scrollTop);


        handleClick(e)
        e.preventDefault();
        console.log('Clicked.');
        this.setState(scrollTop: 0);


        componentDidUpdate()
        this.content.current.scrollTop = this.state.scrollTop;


        render()
        let text = 'annbnncnndnnennfnng';
        return <p><textarea
        ref=this.content
        value=text
        rows="10"
        cols="30"
        onScroll=this.handleScroll/>
        <a href="#" onClick=this.handleClick>
        Scroll to top
        </a></p>;



        ReactDOM.render(
        <App/>,
        document.getElementById('root')
        );

        .as-console-wrapper max-height: 10% !important; 

        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
        <div id="root"></div>





        This isn't particularly useful in this example, but it is useful when you want to use properties instead of local state, as in this answer I posted for my original goal of synchronized scrolling.






        share|improve this answer















        Tommy May explained in a comment that scrollTop isn't an HTML attribute, so I can't link it directly to the component state or properties. However, I found that the componentDidUpdate() method can be used to handle changes to the state or properties.



        Here's an updated example that uses componentDidUpdate():






        class App extends React.Component 
        constructor(props)
        super(props);
        this.state = scrollTop: 0;
        this.content = React.createRef();
        this.handleScroll = this.handleScroll.bind(this);
        this.handleClick = this.handleClick.bind(this);


        handleScroll()
        let scrollTop = this.content.current.scrollTop;
        console.log(scrollTop);
        this.setState(scrollTop: scrollTop);


        handleClick(e)
        e.preventDefault();
        console.log('Clicked.');
        this.setState(scrollTop: 0);


        componentDidUpdate()
        this.content.current.scrollTop = this.state.scrollTop;


        render()
        let text = 'annbnncnndnnennfnng';
        return <p><textarea
        ref=this.content
        value=text
        rows="10"
        cols="30"
        onScroll=this.handleScroll/>
        <a href="#" onClick=this.handleClick>
        Scroll to top
        </a></p>;



        ReactDOM.render(
        <App/>,
        document.getElementById('root')
        );

        .as-console-wrapper max-height: 10% !important; 

        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
        <div id="root"></div>





        This isn't particularly useful in this example, but it is useful when you want to use properties instead of local state, as in this answer I posted for my original goal of synchronized scrolling.






        class App extends React.Component 
        constructor(props)
        super(props);
        this.state = scrollTop: 0;
        this.content = React.createRef();
        this.handleScroll = this.handleScroll.bind(this);
        this.handleClick = this.handleClick.bind(this);


        handleScroll()
        let scrollTop = this.content.current.scrollTop;
        console.log(scrollTop);
        this.setState(scrollTop: scrollTop);


        handleClick(e)
        e.preventDefault();
        console.log('Clicked.');
        this.setState(scrollTop: 0);


        componentDidUpdate()
        this.content.current.scrollTop = this.state.scrollTop;


        render()
        let text = 'annbnncnndnnennfnng';
        return <p><textarea
        ref=this.content
        value=text
        rows="10"
        cols="30"
        onScroll=this.handleScroll/>
        <a href="#" onClick=this.handleClick>
        Scroll to top
        </a></p>;



        ReactDOM.render(
        <App/>,
        document.getElementById('root')
        );

        .as-console-wrapper max-height: 10% !important; 

        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
        <div id="root"></div>





        class App extends React.Component 
        constructor(props)
        super(props);
        this.state = scrollTop: 0;
        this.content = React.createRef();
        this.handleScroll = this.handleScroll.bind(this);
        this.handleClick = this.handleClick.bind(this);


        handleScroll()
        let scrollTop = this.content.current.scrollTop;
        console.log(scrollTop);
        this.setState(scrollTop: scrollTop);


        handleClick(e)
        e.preventDefault();
        console.log('Clicked.');
        this.setState(scrollTop: 0);


        componentDidUpdate()
        this.content.current.scrollTop = this.state.scrollTop;


        render()
        let text = 'annbnncnndnnennfnng';
        return <p><textarea
        ref=this.content
        value=text
        rows="10"
        cols="30"
        onScroll=this.handleScroll/>
        <a href="#" onClick=this.handleClick>
        Scroll to top
        </a></p>;



        ReactDOM.render(
        <App/>,
        document.getElementById('root')
        );

        .as-console-wrapper max-height: 10% !important; 

        <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
        <div id="root"></div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 27 at 4:47

























        answered Mar 26 at 4:46









        Don KirkbyDon Kirkby

        29.6k13 gold badges138 silver badges212 bronze badges




        29.6k13 gold badges138 silver badges212 bronze badges


















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349521%2fset-textarea-scroll-from-reactjs-state%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현