Component - Props passing
This React Native component example shows how to pass data from a parent component to a child component using props. Props are like parameters you give to a child so it can display or use that data.
This React Native component example shows how to pass data from a parent component to a child component using props. Props are like parameters you give to a child so it can display or use that data.
App ├─ View (container) │ ├─ Text (title) │ └─ Greeting (child component) │ └─ Text (message)
props in React Native components?title with value "Hello" to a component Greeting?title="Hello".function Welcome(props) {
return {props.message};
}
export default function App() {
return ;
}Welcome component receives message prop and displays it inside Text.App component passes message="Hi there!" to Welcome.function Button(props) {
return {props.label};
}
export default function App() {
return ;
}label is passed as label=ClickMe without quotes, which is invalid for strings.label="ClickMe" or in curly braces if variable.Profile component that shows a user's name and age. Which of these is the best way to pass props to make it flexible?name should be in quotes; numbers like age should be passed as JavaScript expressions using curly braces.name as string and age as number. <Profile name={"Alice"} age="30" /> passes age as string which is less ideal. <Profile name=Alice age=30 /> misses quotes and braces. <Profile name={Alice} age={30} /> uses undefined variable Alice.Pass the test and this pattern moves unknown