![](/img/trans.png)
[英]How to pass a prop from one component to another component? (React Native)
[英]How to pass prop from one component to another?
我需要將名為 name 的道具從一個 React 組件傳遞到另一個組件,但出了點問題。 你能告訴我如何正確地做嗎?
提前致謝
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;
兩件事情:
export function Greeting(props) {
return (
<h1>Hello World and {props.name}</h1>
)
}
const greetingName = "Alex";
function App() {
return (
<div>
<Greeting name={greetingName} />;
<h2> Welcome, {greetingName} </h2>
</div>
);
}
應用程序.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
問候.js
import React from 'react'
const Greeting = (props) => {
return (
<div>
<h1>Hello World and My name is {props.name}</h1>
</div>
)
}
export default Greeting
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.