Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

javascript - reactjs + webpack + babel 7 Syntax error: The rest element has to be the last element when destructing

I am doing a dev build and I'm getting below babel error. I'm not sure which module/plugin to install for this. Any pointers would be greatly appreciated.

ERROR in ./src/_components/display/DisplayList.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /src/_components/display/DisplayList.jsx: The rest element has to be the last element when destructing (15:23)

  13 | };
  14 |
> 15 | const DisplayList = ({ ...props, children }) => {
     |                        ^
  16 |     const { classes } = props;
  17 |     console.log(props.data)
  18 |     return (

Code used in the file /src/_components/display/DisplayList.jsx

import React from 'react';
import PropTypes from 'prop-types';
import { Typography, withStyles, List } from '@material-ui/core';

const listStyle = {
    heading: {
        textAlign: 'center',
        color: '#FFF',
        backgroundColor: '#383733',
        padding: 10,
        marginBottom: 5,
    }
};

const DisplayList = ({ ...props, children }) => {
    const { classes } = props;
    console.log(props.data)
    return (
        <div>
            <Typography className={classes.heading}>
                {props.title}
            </Typography>
                {children &&
                    children.map((child, key) => {
                        return (
                            <React.Fragment key={key}>
                                {child}
                            </React.Fragment>                    
                        )
                    })}
        </div>
    )
}

DisplayList.propTypes = {
    title: PropTypes.string.isRequired
}

export default withStyles(listStyle)(DisplayList);

Webpack.config.js file

const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    resolve: {
        extensions: ['.js', '.jsx'],
    },        
    devtool: 'cheap-module-source-map',
    module: {
        rules: [
            { 
                test: /.(js|jsx)$/, 
                exclude: /node_modules/, 
                loader: 'babel-loader' 
            },
            {
                test: /.css$/,
                use: ['style-loader', 'css-loader']
            },
            {
                test: /.(gif|png|jpe?g|svg)$/i,
                use: [
                    'file-loader',
                    {
                        loader: 'image-webpack-loader',
                    },
                ],
            }            
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html'
        })
    ]
}

.babelrc file

{
    "presets": [
        "@babel/env", "@babel/react"
    ],
    "plugins": [
        "@babel/proposal-class-properties",
        "@babel/transform-react-inline-elements",
        "@babel/transform-react-constant-elements",
        "@babel/proposal-object-rest-spread",
        "@babel/transform-spread"
    ]
}

I could'nt find much relevant information in the internet that addresses this problem. Any pointers of where I could have gone wrong ? Is there any babel plugin/module to be installed to get this fixed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just like the error message says, move ...props as the last element:

const DisplayList = ({ children, ...props }) => (...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...