如何将道具从一个组件传递到另一个组件?

发布于 2025-01-26 01:23:13 字数 582 浏览 2 评论 0原文

我需要将一个名为名称的Prop从一个反应组件传递到另一个反应组件,但是出了问题。你能告诉我怎么做正确的吗?

提前致谢

import React from 'react';

export class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello World and {this.props.name}</h1>
    )
  }
}
import React from 'react';
import ReactDOM from 'react-dom';
import Greeting from './Greeting';

function App() {
  return (
    <div>
      <Greeting name='Alex' />;
      <h2> Welcome, {Greetings.props.name} </h2>
    </div>
  );
}

export default App;

I need to pass prop called name from one React component to another, but something goes wrong. Could you tell me how to do it right?

Thanks in advance

import React from 'react';

export class Greeting extends React.Component {
  render() {
    return (
      <h1>Hello World and {this.props.name}</h1>
    )
  }
}
import React from 'react';
import ReactDOM from 'react-dom';
import Greeting from './Greeting';

function App() {
  return (
    <div>
      <Greeting name='Alex' />;
      <h2> Welcome, {Greetings.props.name} </h2>
    </div>
  );
}

export default App;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里南柯 2025-02-02 01:23:13

两件事:

  1. 看起来您正在混合班级和功能性组件,我建议现在更常见的是功能。在这种情况下,问候看起来像这样:
export function Greeting(props) {
  return (
    <h1>Hello World and {props.name}</h1>
  )
}
  1. 您正在传递组件的道具,但是您无法从父母那里访问它。您可以为此创建另一个变量:
const greetingName = "Alex";

function App() {
  return (
    <div>
      <Greeting name={greetingName} />;
      <h2> Welcome, {greetingName} </h2>
    </div>
  );
}

two things:

  1. looks like you are mixing class and functional components, i recommend going for functional as its more common now. in that case the Greeting will look like this:
export function Greeting(props) {
  return (
    <h1>Hello World and {props.name}</h1>
  )
}
  1. you are passing the prop the the component, but you cant access it from its parent. you can create another variable for that:
const greetingName = "Alex";

function App() {
  return (
    <div>
      <Greeting name={greetingName} />;
      <h2> Welcome, {greetingName} </h2>
    </div>
  );
}
浮云落日 2025-02-02 01:23:13

在engreting.js中,您错过了参数

app.js

import React, { useState } from 'react';
import Greeting from './Greeting';

const App = () => {
  const [name, setName] = useState("")
  return (
    <div>
      <Greeting name="Alex" />
    </div>
  )
}

export default App

engreting.js

import React from 'react'

const Greeting = (props) => {
    return (
        <div>
            <h1>Hello World and My name is {props.name}</h1>
        </div>
    )
}

export default Greeting

In Greeting.js you have missed parameter

App.js

import React, { useState } from 'react';
import Greeting from './Greeting';

const App = () => {
  const [name, setName] = useState("")
  return (
    <div>
      <Greeting name="Alex" />
    </div>
  )
}

export default App

Greeting.js

import React from 'react'

const Greeting = (props) => {
    return (
        <div>
            <h1>Hello World and My name is {props.name}</h1>
        </div>
    )
}

export default Greeting
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文