I have two components one called ReturningCustomer and the other one called Update. I have a phone number saved in state in ReturningCustomer and I want to pass that phone number to the Update component with having to render Update or use Redux:
render() {
return (
<div className="row returning-customer">
<h3>Returning Customer</h3>
<div className="col">
<p className="error">{this.state.error}</p>
<form className="row" onSubmit={this.onSubmit}>
<label>Phone Number</label>
<input
type="text"
className="validate"
onChange={this.onPhoneNumberChange}
/>
<button className="btn">Go</button>
</form>
</div>
<Update
phoneNumber={this.state.phoneNumber}
/>
</div>
);
}
Nope, render() method can't return an empty array or anything other than other React component.
To pass props, add them to the JSX, just like you would with HTML attributes. To read props, use the function Avatar({ person, size }) destructuring syntax. You can specify a default value like size = 100 , which is used for missing and undefined props.
export default App; Basically that's how props are passed from component to component in React. As you may have noticed, props are only passed from top to bottom in React application's component hierarchy. There is no way to pass props up to a parent component from a child component.
Props are not affected by the state change, so heavy components won't re-render.
In the render function for Update you could use either one of the following and the component would not render
render() {
return false;
}
render() {
return null;
}
render() {
return [];
}
render() {
return <></>;
}
It's not entirely clear what you're asking based on the current description, but it sounds like you're either wanting to conditionally render a component while passing props, or you want to know how to use those props? I'm including both here.
Conditionally rendering a component and passing props:
// SomeComponent.js
import React from 'react'
import Update from '../..'
class SomeComponent extends React.Component {
state = { phoneNumber: '' };
renderUpdate() {
const someCondition = true;
if (someCondition) {
return <Update phoneNumber={this.state.phoneNumber} />
}
return null
}
render() {
return (
<div>
{this.renderUpdate()}
</div>
)
}
}
export default SomeComponent
Using props:
// Update.js
import React from 'react'
// destructuring the props object here
const Update = ({ phoneNumber }) => (
<div>
<p>{phoneNumber}</p>
</div>
)
export default Update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With