简体   繁体   English

使用create-react-app会使变量超出范围

[英]Using create-react-app throws variables out of scope

I'm following a tutorial in O'Reilly's Learning React. 我正在关注O'Reilly的Learning React中的教程。 I'm trying to build the recipes app that is on page 95 or so. 我正在尝试构建第95页左右的食谱应用程序。

Because the code in this book bridges React 15 and 16 and who knows what version of Webpack I decided to take their suggestions and build my app using create-react-app. 因为本书中的代码是React 15和React 16的桥梁,并且谁知道Webpack的版本,所以我决定采纳他们的建议,并使用create-react-app构建我的应用程序。

I've been successful in changing over most of the code to work with cra's structure, but I can't fix the code in this component: 我已经成功更改了大多数代码以使其与cra的结构兼容,但是我无法在此组件中修复代码:

import React from 'react';
import ReactDOM from 'react-dom';

const Instructions = ({ title, steps }) =>
  <section className="instructions">
    <h2>{title}</h2>
    {steps.map}((s, i) =>
      <p key={i}>{s}</p>
    )
  </section>

export default Instructions

The error I get is: 我得到的错误是:

Failed to compile
./src/components/Instructions.js
  Line 10:  'i' is not defined  no-undef
  Line 10:  's' is not defined  no-undef

I've tried explicitly setting those variables using var and disabling the ESLinter by line, but neither works. 我试过使用var显式设置这些变量,并逐行禁用ESLinter,但均无效。 In addition to solving the problem, I'd like to understand why this worked with Webpack and straight JSX but breaks with create-react-app. 除了解决问题之外,我还想了解为什么此方法可与Webpack和纯JSX一起使用,却无法与create-react-app一起使用。

The brackets serves as a escape from JSX to JavaScript. 括号用作从JSX到JavaScript的转义符。 In this case you're closing it in the wrong place. 在这种情况下,您会将其关闭在错误的位置。 Try: 尝试:

const Instructions = ({ title, steps }) =>
  <section className="instructions">
  <h2>{title}</h2>
    {steps.map((s, i) =>
      <p key={i}>{s}</p>
    )}
  </section>

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

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