In this unit, you create the MortgageCalculator module as well as the modules it depends on.
Create a new file named mortgage.js
in the js
directory.
Move the calculatePayment()
function from app.js
into mortgage.js
.
Add export
in front of the function definition to make it available as part of the module public API:
export let calculatePayment = (principal, years, rate) => {
Create a new file named AmortizationChart.js
in the js
directory.
Import the react module:
import React from 'react';
Move the AmortizationChart
class definition from app.js
into AmortizationChart.js
.
At the bottom of the file, export the class as the default export:
export default AmortizationChart;
Create a new file named MortgageCalculator.js
in the js
directory.
Add the following import statements at the top of the file:
import React from 'react';
import AmortizationChart from './AmortizationChart';
import * as mortgage from './mortgage';
Move the MortgageCalculator
class definition from app.js
into MortgageCalculator.js
.
In the render()
function, change calculatePayment()
to mortgage.calculatePayment()
At the bottom of the file, export the class as the default export:
export default MortgageCalculator;
Open app.js
and add an import statement for the MortgageCalculator module:
import MortgageCalculator from './MortgageCalculator';
app.js
should now look like this:
import React from 'react';
import ReactDOM from 'react-dom';
import Header from './Header';
import MortgageCalculator from './MortgageCalculator';
class App extends React.Component {
render() {
return (
<div>
<Header title="React ES6 Mortgage Calculator"/>
<MortgageCalculator principal="200000" years="30" rate="5"/>
</div>
);
}
};
ReactDOM.render(<App/>, document.getElementById("app"));
Build the application:
npm run webpack
Open index.html in your browser and test the application
The final code for this step is available in this branch.