-1

I am learning React and wondered if there was a way to re-render on state change without using React. Is this possible in plain JavaScript?

James Monger
  • 10,181
  • 7
  • 62
  • 98
D. Cantatore
  • 187
  • 12

1 Answers1

0

Let's assume that our whole application is this:

<input id="myState" />
<h2 id="myView"></h2>

We have some sort of value we call state and we have some sort of value we call view. Now what we want is that when we update our state, we want to update our view. Yes, that is absolutely possible without React/Redux/etc.

window.onload = function(){
    var state = document.getElementById('myState'),
            view = document.getElementById('myView')
    state.addEventListener('keyup',function(e){
        view.innerText = e.target.value
    })
}

Every time the 'state' of our application changes, we are resetting the 'view'.

Going past this dummy problem you will run into alot of issues, which are the issues that React/Angular/Ember/Backbone/etc all try to help fix.

Can it be done in vanilla JS? Yes, because someone else already did it in vanilla JS.

Tim Roberts
  • 1,120
  • 2
  • 9
  • 25
  • I discussed this at a meetup last night and they had a similar answer to yours. React/Redux makes it easier but it's all javascript either way so technically you could write you own mini framework to strip the feature that you want out but you should really just use the framework that does this as long as it isn't too bloated. – D. Cantatore Oct 06 '16 at 14:01
  • I would say it really depends on how crazy your UI is. If it's just a list of ~100 elements that you want to filter/dedraw, it's simple to just filter and repaint in vanilla JS. If you are wanting a full fledged SPA, it gets more fun but then you end up building the libraries over again. Give for what you get. – Tim Roberts Oct 06 '16 at 18:17