This project is a simple To Do List example of how to go about incrementally refactoring your JavaScript application to the React framework and how you can use Redux to share state between React and your original JavaScript.
The default branch, 'master', is only the working jQuery To Do List. There are additional branches to track the examples, see the branches explanation below. The refactoring steps are available for you via tags, or you may choose to view the project diffs by viewing the open pull requests.
This example was originally presented as part of a talk on "Refactoring a Legacy Application with React" given at CodeMash 2018. Slides for the presentation are available at https://speakerdeck.com/aporlando/refactoring-a-legacy-application-with-react.
-
master [default]
This branch is the starting point - a working To Do list application using only plain JavaScript and some jQuery
-
refactor
This branch includes examples of refactoring the 'add to do item' input and 'Remove Completed' items button to React (without Redux)
-
withRedux
This branch includes all the same content as the refactor branch and updates the 'Remove Completed' items button to use Redux state
The purpose of this example is to show how you can begin to add React components to your front-end. The steps used in the refactor:
- Start trying out React right away by adding UMD scripts to your HTML file, you may also refer to the example HTML template provide in the React Docs
- Create the new AddTodoInput component by borrowing the markup that was in the DOM as our JSX
- Render the new component into the DOM using the ReactDOM library
- Handle the input's onChange and onSubmit using React state in steps:
- Add the initial state to the component
- Handle the onChange of the input, see React's guide on handling forms
- Handle the onSubmit using React state
- Remove the old jQuery onsubmit event listener
- Create a new 'Remove Completed' button with a prop to toggle visibility
- Use ReactDOM's render to update the button's prop when jQuery needs to toggle the visibility of the button
The purpose of the Redux example is to show an alternative way to handle state in React that needs to be updated by an external library. In the previous section, we used ReactDOM to update the 'show' prop in the button. This shows how you can manage the button's show prop using Redux for application level state.
- Add the Redux and React-Redux UMD scripts to the HTML page
- Define the initial state we want to track
- Define the action object that will describe what happens when the visibility should be toggled
- Define a pure reducer function that returns the new state when it receives the action defined in step 2
- Use the Redux library to create and manage the store
- Connect the RemoveCompletedButton component to the redux store using the react-redux connect component
- Dispatch the action to toggle the visibility in our jQuery code