【问题标题】:How to fix arrow function in React.js to work with props如何修复 React.js 中的箭头函数以使用道具
【发布时间】:2019-04-14 06:56:07
【问题描述】:

我正在尝试在 localhost:3000 上打开一个简单的网页,并且正在使用 React.js - 目前似乎使用 React.js,当我键入“=”时它无法识别我的箭头函数>' 并且我已经检查了我正在遵循的教程,以确保我没有输入任何拼写错误 - 还有其他人遇到过 React.js 和箭头函数的问题吗?

有趣的是,它在我正在观看的教程中运行良好,

非常感谢!

重新检查代码,检查 App.js,谷歌搜索 React.js 中的箭头函数错误

Greet.js 代码如下:

import React from 'react'

// function Greet() {
//     return <h1>Hey Henry!</h1>
// }

const Greet = props () => {
    console.log(props)
    return <h1>Aloha you nutter! How's it going {props.name}?</h1>}

export default Greet

App.js 代码如下:

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import Greet from './components/Greet'
import Hello from './components/Hello'

class App extends Component {
  render() {
    return (
      <div className="App">
        {/*<header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            HELLO WORLD!!!
            Henry's first React program!
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
    </header>*/}
      <Greet name="Henry" />
      <Greet name="Adrian" />
      <Greet name="Lordi" /> 
      {/*<Hello />*/}


      </div>
    );
  }
}

export default App;

预期结果:

在 localhost:3000 上显示的网页显示“Aloha you nutter!它进展如何{name property goes here}?” 3 次使用 3 个不同的名称。

【问题讨论】:

标签: javascript reactjs ecmascript-6 arrow-functions react-props


【解决方案1】:

试试这样:

const Greet = (props) => {
    console.log(props)
    return <h1>Aloha you nutter! How's it going {props.name}?</h1>}

export default Greet

props 是箭头函数中的参数。

【讨论】:

  • 谢谢大家的帮助:-)
猜你喜欢
  • 2018-11-24
  • 2019-01-17
  • 1970-01-01
  • 2021-05-14
  • 2019-01-10
  • 1970-01-01
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多