Skip to content

Instantly share code, notes, and snippets.

@xyzdata
Last active June 26, 2017 07:26
Show Gist options
  • Save xyzdata/1d912ebf4045f9dcc0747cb761f62090 to your computer and use it in GitHub Desktop.
Save xyzdata/1d912ebf4045f9dcc0747cb761f62090 to your computer and use it in GitHub Desktop.
`props` & `route.data` & `this.props.route.data`

React components & react-router-dom

props & route.data

const cars = this.props.route.data;

car

    
import React, { Component } from 'react';
import { Link } from 'react-router';

class Car extends Component {
    render(){
        // Get data from route props
        const cars = this.props.route.data;
        console.log(`cars = ${cars}`);
        console.log(`cars[0] = ${cars[0]}`);
        console.log(`cars make = ${cars[0].make}`);
        console.log(`cars price = ${cars[0].price}`);
        console.log(`cars make = ${cars[1].make}`);
        console.log(`cars price = ${cars[1].price}`);
        // Map through cars and return linked cars
        const carNode = cars.map((car) => {
            return (
                <Link
                    to={"/cars/"+car.id}
                    className="list-group-item"
                    key={car.id}>
                    {car.name}
                </Link>
            )
        });
        return (
            <div>
                <h1>Cars page</h1>
                <div className="list-group">
                    {carNode}
                </div>
            </div>
        );
    }
}

export default Car;

cars details

    
import React, { Component } from 'react';
import { browserHistory } from 'react-router';

class CarDetail extends Component {
    handleRedirect(){
        browserHistory.push('/cars');
    }
    render(){
        const cars = this.props.route.data;
        // data
        const id = this.props.params.id;
        // params
        console.log(`&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&`);
        console.log(`typeof id = ${typeof(id)}`);
        console.log(`params.id = ${id}`);
        
        // number
        const car = cars.filter(car => {
            console.log(`******************************************************`);
            console.log(`car.id = ${car.id}`);
            console.log(`typeof car.id = ${typeof(car.id)}`);
            // number
            if(car.id == id) {
                return car;
            }
        });
        return (
            <div>
                <h1>{car[0].name}</h1>
                <div className="row">
                    <div className="col-sm-6 col-md-4">
                        <div className="thumbnail">
                            <img src={car[0].media} alt={car[0].name} />
                        </div>
                    </div>
                    <div className="col-sm-6 col-md-4">
                       <ul>
                           <li><strong>Model</strong>: {car[0].model}</li>
                           <li><strong>Make</strong>: {car[0].make}</li>
                           <li><strong>Year</strong>: {car[0].year}</li>
                           <li><strong>Price</strong>: {car[0].price}</li>
                       </ul>
                    </div>
                    <div className="col-md-12">
                        <button className="btn btn-default" onClick={this.handleRedirect.bind(this)}>Go to Cars</button>
                    </div>
                </div>
            </div>
        );
    }
}

export default CarDetail;

index

    
function Welcome(props) {
    return (
        <Router history={browserHistory}>
            <Route component={Main}>
                <IndexRoute component={Home} />
                <Route path="/" component={Home}/>
                {/*<IndexRoute component={Home} />*/}
                <Route path="/cars" component={Car} data={data}/>
                {/* Parameter route*/}
                <Route path="/cars/:id" component={CarDetail} data={data}/>
                <Route path="/about" component={About}/>
            </Route>
        </Router>
    );
}

datas

const data = [
    {
        id: 1,
        name: 'Honda Accord Crosstour',
        year: '2010',
        model: 'Accord Crosstour',
        make: 'Honda',
        media: 'http://media.ed.edmunds-media.com/honda/accord-crosstour/2010/oem/2010_honda_accord-crosstour_4dr-hatchback_ex-l_fq_oem_4_500.jpg',
        price: '$16,811'

    },
    {
        id: 2,
        name: 'Mercedes-Benz AMG GT Coupe',
        year: '2016',
        model: 'AMG',
        make: 'Mercedes Benz',
        media: 'http://media.ed.edmunds-media.com/mercedes-benz/amg-gt/2016/oem/2016_mercedes-benz_amg-gt_coupe_s_fq_oem_1_717.jpg',
        price: '$138,157'

    },
    {
        id: 3,
        name: 'BMW X6 SUV',
        year: '2016',
        model: 'X6',
        make: 'BMW',
        media: 'http://media.ed.edmunds-media.com/bmw/x6/2016/oem/2016_bmw_x6_4dr-suv_xdrive50i_fq_oem_1_717.jpg',
        price: '$68,999'
    },
    {
        id: 4,
        name: 'Ford Edge SUV',
        year: '2016',
        model: 'Edge',
        make: 'Ford',
        media: 'http://media.ed.edmunds-media.com/ford/edge/2016/oem/2016_ford_edge_4dr-suv_sport_fq_oem_6_717.jpg',
        price: '$36,275'
    },
    {
        id: 5,
        name: 'Dodge Viper Coupe',
        year: '2017',
        model: 'Viper',
        make: 'Dodge',
        media: 'http://media.ed.edmunds-media.com/dodge/viper/2017/oem/2017_dodge_viper_coupe_acr_fq_oem_3_717.jpg',
        price: '$123,890'
    }
];
    
@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

cmd cache error & webpack --watch/ -w

webpack-windows-cmd-cache-size-error

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

browserHistory

https://reacttraining.com/react-router/web/api/history

https://github.com/ReactTraining/history#blocking-transitions

https://github.com/ReactTraining/history

push(path, [state]) - (function)

Pushes a new entry onto the history stack

    handleRedirect(){
        browserHistory.push('/cars');
        // navigation
    }

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

js-get-html5-input-value

image

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

input type null === text

input type === null & input type === text

https://developer.mozilla.org/zh-CN/docs/Web/API/HTMLElement/style

https://developer.mozilla.org/zh-CN/docs/Web/API/Element/getAttribute

https://developer.mozilla.org/zh-CN/docs/Web/API/Element/setAttribute

element.style;

element.style.color;
element.style.color = "red";


element.getAttribute(attributeName);

element.setAttribute(name, value);

image

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

object length

length-of-a-javascript-object

https://stackoverflow.com/questions/5223/length-of-a-javascript-object

https://stackoverflow.com/a/5527037/8202629

Object.keys(obj).length;

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myArray);

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

Object.keys(obj).length & Object.keys(array).length

// Object.keys(obj).length

// Object.keys(array).length

// Array.length;

data.length;
// 5

Object.keys(data).length;
// 5


let json = data[0];

Object.keys(json).length
// 7
const data = [
    {
        id: 1,
        name: 'Honda Accord Crosstour',
        year: '2010',
        model: 'Accord Crosstour',
        make: 'Honda',
        media: 'http://media.ed.edmunds-media.com/honda/accord-crosstour/2010/oem/2010_honda_accord-crosstour_4dr-hatchback_ex-l_fq_oem_4_500.jpg',
        price: '$16,811'

    },
    {
        id: 2,
        name: 'Mercedes-Benz AMG GT Coupe',
        year: '2016',
        model: 'AMG',
        make: 'Mercedes Benz',
        media: 'http://media.ed.edmunds-media.com/mercedes-benz/amg-gt/2016/oem/2016_mercedes-benz_amg-gt_coupe_s_fq_oem_1_717.jpg',
        price: '$138,157'

    },
    {
        id: 3,
        name: 'BMW X6 SUV',
        year: '2016',
        model: 'X6',
        make: 'BMW',
        media: 'http://media.ed.edmunds-media.com/bmw/x6/2016/oem/2016_bmw_x6_4dr-suv_xdrive50i_fq_oem_1_717.jpg',
        price: '$68,999'
    },
    {
        id: 4,
        name: 'Ford Edge SUV',
        year: '2016',
        model: 'Edge',
        make: 'Ford',
        media: 'http://media.ed.edmunds-media.com/ford/edge/2016/oem/2016_ford_edge_4dr-suv_sport_fq_oem_6_717.jpg',
        price: '$36,275'
    },
    {
        id: 5,
        name: 'Dodge Viper Coupe',
        year: '2017',
        model: 'Viper',
        make: 'Dodge',
        media: 'http://media.ed.edmunds-media.com/dodge/viper/2017/oem/2017_dodge_viper_coupe_acr_fq_oem_3_717.jpg',
        price: '$123,890'
    }
];

@xyzdata
Copy link
Author

xyzdata commented Jun 26, 2017

css-loader & style-loader

$ npm i -D css-loader style-loader

$ npm install --save-dev css-loader style-loader

import 'bootstrap/dist/css/bootstrap.css';

module.exports = {
    module: {
        rules: [{
            test: /\.css$/,
            use: [ 'style-loader', 'css-loader' ]
        }]
    }
}

https://webpack.js.org/guides/code-splitting-css/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment