D. Cantatore D. Cantatore - 1 year ago 75
React JSX Question

State in JavaScript without React

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?

Answer Source

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')
        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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download