Learning Full-Stack JavaScript Development: MongoDB, Node and React


4. Working with Data


015 Loading the test data

$ npm install --save json-loader


<br/>

webpack.config.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
const path = require('path');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.join(__dirname, 'public'),
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                loader: 'json-loader',
                test: /\.json$/
            },
            {
                loader: 'babel-loader',
                test: /\.js$/,
                exclude: /node_modules/
            }
        ]
    },
    devtool: 'cheap-module-eval-source-map'
};


016 Displaying a list of objects


index.js

1
2
3
4
5
6
7
8
9
10
import React from 'react';
import ReactDOM from 'react-dom';

import data from './testData';
import App from './components/App';

ReactDOM.render(
    <App contests={data.contests} />,
    document.getElementById('root')
);


App.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import React from 'react';
import Header from './Header';
import ContestPreview from './ContestPreview';

class App extends React.Component {
    
    constructor(props){
        super(props);
        this.pageHeader = 'Naming Contests';
        this.state = { test: 42 };
    }
        
    componentDidMount() {
        
    }
    
    componentWillUnmount() {
        
    }

    render() {
        return (
            <h2 className="App">
                <Header message= {this.pageHeader} />
                <div>        
                    {this.props.contests.map(contest =>                        
                        <ContestPreview {...contest} />
                    )}
                </div>
            </h2>
        );
    }
};

export default App;


017 Using Sass with Node

$ npm install --save node-sass-middleware


server.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import config from './config';
import apiRouter from './api';
import sassMiddleware from 'node-sass-middleware';
import path from 'path';

import express from 'express';
const server = express();

server.use(sassMiddleware({
    src: path.join(__dirname, 'sass'),
    dest: path.join(__dirname, 'public')
}));

server.set('view engine', 'ejs');

server.get('/', (req, res) => {
    res.render('index', {
        content: 'Hello Express and <em>EJS!</em>'
    });
});

server.get('/about.html', (req, res) => {
    res.send('The about page');
});


server.use('/api', apiRouter);
server.use(express.static('public'));

server.listen(config.port, () => {
    console.info('Express listening on port ', config.port);
});


018 Reading from the state


index.js

1
2
3
4
5
6
7
8
9
import React from 'react';
import ReactDOM from 'react-dom';

import App from './components/App';

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


App.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import React from 'react';
import Header from './Header';
import ContestPreview from './ContestPreview';
import data from '../testData';

class App extends React.Component {
    
    constructor(props){
        super(props);
        this.pageHeader = 'Naming Contests';
        this.state = { test: 42 };
        this.contests = data.contests;
    }
        
    componentDidMount() {
        // this.contests = data.contests;
    }
    
    componentWillUnmount() {
        
    }

    render() {  
              
        return (
            <h2 className="App">
                <Header message= {this.pageHeader} />
                <div>        
                    {this.contests.map(contest =>                        
                        <ContestPreview key={contest.id} {...contest} />
                    )}
                </div>
            </h2>
        );
    }
};

export default App;