简体   繁体   English

React / Redux项目未评估带括号的ES6

[英]React/Redux project not evaluating bracketed ES6

SOLVED: I'D ADDED YARN TO THE FILE. 求助:我已经在文件中添加了纱。 I DELETED IT AND THE MODULES THEN REDID NPM INSTALL AND IT WORKED... not exactly sure what happened there, but that seemed to do it. 我删除了它,然后删除了模块,然后重新安装了NPM并开始工作...不完全确定那里发生了什么,但这似乎可以解决。

EDIT: SO IT TURNS OUT THE STRING INTERPOLATION IS NOT RELATED (using single quotes instead of back ticks - face palm) BUT THE ISSUE WITH THE CASE STATEMENT NOT BEING ABLE TO BE EVALUATED IS STILL AT LARGE. 编辑:事实证明,与插值不相关(使用单引号而不是反引号-脸掌),但无法评估案例陈述的问题仍然很大。

So I'm doing Wes Bos' Redux Course , which is pretty cool and I like it so far even if it is A LOT to take in very quickly. 所以我正在做Wes Bos的Redux课程 ,这很酷,到目前为止,我很喜欢,即使很快就可以参加。

There have been some very few things I needed to update here and there in order to get it to work. 为了使它正常工作,我需要在这里和那里进行一些更新。 (Such as this line in my main component, without which Hot Reloading wasn't working at all: module.hot.accept() , but which wasn't in his files ) (例如在我的主要组件中的这一行,没有它,则热重装根本无法工作:module.hot.accept(),但是在他的文件中却没有)

I'm also extending the React.Component rather than using .createClass to try and be more current. 我也在扩展React.Component而不是使用.createClass尝试并使其更具最新性。 Otherwise my files are by and large exactly the same as his files linked above. 否则,我的文件与上面链接的文件大致相同。

I'm up to about the 13th video and I've got a problem that has stopped me completely, which I can't get past, which I only have an probably wrong idea about, but I'm hoping someone can help. 我正在看第13部影片,但有一个问题使我完全停滞不前,这是我无法逾越的,这只是个错误的主意,但我希望有人能提供帮助。

Firstly, I think this other more trivial problem may be related, my components will not do string interpolation as his will: 首先,我认为这可能与其他更琐碎的问题有关,我的组件不会像他的意愿那样进行字符串插值:

import React from 'react';
import { Link } from 'react-router';
import CSSTransitionGroup from 'react-addons-css-transition-group';


export default class Photo extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    const { post, index, comments } = this.props

    return (
      <figure className="grid-figure">
        <div className="grid-photo-wrap">

          <Link to={'/view/${post.code}'}> 
            <img className="grid-photo" src={post.display_src} alt={post.caption} />
          </Link> ...

This totally works for him except this will not work for me. 这对他完全有效,除非对我不起作用。 The link target will not be evaluated, I will have to use: 链接目标将不会被评估,我将不得不使用:

<Link to={'/view/' + post.code}>

Or else the href will come out like this: 否则,href将像这样出来:

http://localhost:7770/view/$%7Bpost.code%7D

Not able to find a reason why that wasn't working, I used the ugly string addition thing and moved on. 无法找到无法解决问题的原因,我使用了丑陋的字符串添加功能并继续前进。 But then I hit a stopping problem a few videos later that strikes me as similar, and may be related, so I'm asking about it here. 但是后来我在几个视频上遇到了一个停顿的问题,令我震惊,并且可能是相关的,所以我在这里问这个问题。

In his 12th video he fleshes out the posts reducer with this code: 在他的第12个视频中,他使用以下代码充实了减速器:

function posts(state=[], action) {
  switch(action.type) {
    case 'INCREMENT_LIKES':
    const index = action.index;
      return [
        ...state.slice(0,index), // before what we are updating
        {...state[index], likes: state[index].likes + 1},
        ...state.slice(index + 1), // after what we are updating
      ]
    default:
      return state;
  }
}

export default posts;

This works for him as intended. 这按预期对他有效。 For me, not at all. 对我而言,一点也不。 I get: 我得到:

    5 |       return [
    6 |         ...state.slice(0,index), // before what we are updating
 >  7 |         {...state[index], likes: state[index].likes + 1},
      |          ^
    8 |         ...state.slice(index + 1), // after what we are updating
    9 |       ]
   10 |     default:

My guess is that for some reason in both cases the injected ES6 is not being evaluated. 我的猜测是,由于某种原因,在两种情况下都不会评估注入的ES6。 Though both these work separately elsewhere because everything else in the app relying on either injected JS or ES6 up until this point is working fine. 尽管这两种方法在其他地方都可以单独使用,因为到目前为止,应用程序中的所有其他内容都依赖于注入的JS或ES6。 For instance, in the first example post.display_src is evaluated fine inside brackets, as is the constant assignment using destructuring. 例如,在第一个示例中,post.display_src在方括号内被很好地评估,而使用解构的常量赋值也是如此。 So I don't know if the culprit is ES6 inside brackets. 所以我不知道罪魁祸首是括号内的ES6。 But that is my only idea, and as far as I can take it. 但这是我唯一的想法,并且据我所知。 Hopefully someone can bail me out so I can continue the tutorial. 希望有人可以帮助我,以便我继续本教程。

PS - like I said my files are essentially identical to his except for the small changes mentioned. PS-就像我说的,除了提到的一些小改动外,我的文件与他的文件基本相同。 Still I'll include here the webpack.config.dev.js because I anticipate it may be lots of people's first port of call: 我仍将在此处包括webpack.config.dev.js,因为我希望它可能是很多人的第一个呼叫口:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  devtool: 'source-map',
  entry: [
    'webpack-hot-middleware/client',
    './client/rootMount'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    publicPath: '/static/'
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoErrorsPlugin(),
  ],
  module: {
    loaders: [
    {
      test: /\.js$/,
      loaders: ['babel'],
      include: path.join(__dirname, 'client'),
    },
    { 
      test: /\.styl$/, 
      include: path.join(__dirname, 'client'),
      loader: 'style-loader!css-loader!stylus-loader'
    }
    ]
  }
};

And the .babelrc file: 和.babelrc文件:

{
  "presets": ["react", "es2015"],
  "env": {
    "development": {
      "plugins": [
        ["transform-object-rest-spread"],
        ["transform-react-display-name"],
        ["react-transform", {
          "transforms": [{
            "transform": "react-transform-hmr",
            "imports": ["react"],
            "locals": ["module"]
          }, {
            "transform": "react-transform-catch-errors",
            "imports": ["react", "redbox-react"]
          }]
        }]
      ]
    },
    "production": {
      "plugins": [
        ["transform-object-rest-spread"],
        ["transform-react-display-name"]
      ]
    }
  }
}

Looks like your code is not using backticks go from this: 看起来您的代码未使用反引号来自此:

'/view/${post.code}'  

to this: 对此:

`/view/${post.code}`

Ignacio's answer should help you with the first issue, remember that it is javascript and you entered ${post.code} as if it were string you can do what Ignacio did or just do <Link to={'/view/' + post.code }> Ignacio的答案应该可以帮助您解决第一个问题,请记住,它是JavaScript,并且您输入了${post.code} ,就好像它是字符串一样,您可以执行Ignacio的操作,也可以只做<Link to={'/view/' + post.code }>

As for your reducer when using ES6 and switches I found it necessary to wrap the case's code in brackets {} so change your switch to 至于使用ES6和开关时的减速器,我发现有必要将案例的代码包装在方括号{}因此将开关更改为

function posts(state=[], action) {
  switch(action.type) {
    case 'INCREMENT_LIKES':{
    const index = action.indes;
      return [
        ...state.slice(0,index), // before what we are updating
        {...state[index], likes: state[index].likes + 1},
        ...state.slice(index + 1), // after what we are updating
      ]}
    default:
      return state;
  }
}

export default posts;

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM