react props explained code example
Example 1: props in react
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" /> <Welcome name="Cahal" /> <Welcome name="Edite" /> </div>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
Example 2: render props
//Render props are functions that are passed to props as values
const User = (props) => {
const [name, setName] = useState('John')
//we pass values to prop-function as args
return <div>{props.render(name)}</div>
}
//Then we can use this component like this:
<div>
//when using the component we receive the passed values (name) as args here
<User render={(name) => <div>{name}</div>} />
</div>
Example 3: props in react app
/* PASSING THE PROPS to the 'Greeting' component */
const expression = 'Happy';
<Greeting statement='Hello' expression={expression}/> // statement and expression are the props (ie. arguments) we are passing to Greeting component
/* USING THE PROPS in the child component */
class Greeting extends Component {
render() {
return <h1>{this.props.statement} I am feeling {this.props.expression} today!</h1>;
}
}
--------------------------------------------
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);
Example 4: react props
Props is a way to transform data from one component to another.
Example 5: props react
function Welcome(props) { return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;ReactDOM.render(
element,
document.getElementById('root')
);