score:5
Accepted answer
You need to guard against it not existing. There are a couple of ways to do that. For instance, the conditional operator:
<div className="col-1">{props.personTo ? props.personTo.firstName : ""}</div>
<div className="col-1">{props.personTo ? props.personTo.lastName : ""}</div>
Live Example:
const Document = props => {
console.log(JSON.stringify(props));
return(
<div className="row">
<div className="col-3">{props.dateOnLetter}</div>
<div className="col-1">{props.personFrom.firstName}</div>
<div className="col-1">{props.personFrom.lastName}</div>
<div className="col-1">{props.personTo ? props.personTo.firstName : ""}</div>
<div className="col-1">{props.personTo ? props.personTo.lastName : ""}</div>
</div>
);
};
ReactDOM.render(
<Document dateOnLetter="today" personFrom={{firstName: "Joe", lastName: "Bloggs"}} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Another option is to destructure your props
on receipt and provide a default for the personTo
property:
const Document = ({dateOnLetter, personFrom, personTo = {firstName: "", lastName: ""}}) => {
return(
<div className="row">
<div className="col-3">{dateOnLetter}</div>
<div className="col-1">{personFrom.firstName}</div>
<div className="col-1">{personFrom.lastName}</div>
<div className="col-1">{personTo.firstName}</div>
<div className="col-1">{personTo.lastName}</div>
</div>
);
};
Live Example:
const Document = ({dateOnLetter, personFrom, personTo = {firstName: "", lastName: ""}}) => {
return(
<div className="row">
<div className="col-3">{dateOnLetter}</div>
<div className="col-1">{personFrom.firstName}</div>
<div className="col-1">{personFrom.lastName}</div>
<div className="col-1">{personTo.firstName}</div>
<div className="col-1">{personTo.lastName}</div>
</div>
);
};
ReactDOM.render(
<Document dateOnLetter="today" personFrom={{firstName: "Joe", lastName: "Bloggs"}} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
You don't have to destructure in the parameter list if you don't want to, and you don't have to destructure everything if you don't want to:
const Document = props => {
const {personTo = {firstName: "", lastName: ""}} = props;
return(
<div className="row">
<div className="col-3">{props.dateOnLetter}</div>
<div className="col-1">{props.personFrom.firstName}</div>
<div className="col-1">{props.personFrom.lastName}</div>
<div className="col-1">{personTo.firstName}</div>
<div className="col-1">{personTo.lastName}</div>
</div>
);
};
Live Example:
const Document = props => {
const {personTo = {firstName: "", lastName: ""}} = props;
return(
<div className="row">
<div className="col-3">{props.dateOnLetter}</div>
<div className="col-1">{props.personFrom.firstName}</div>
<div className="col-1">{props.personFrom.lastName}</div>
<div className="col-1">{personTo.firstName}</div>
<div className="col-1">{personTo.lastName}</div>
</div>
);
};
ReactDOM.render(
<Document dateOnLetter="today" personFrom={{firstName: "Joe", lastName: "Bloggs"}} />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
score:1
As Chris Cousins said in a comment, default props are another option:
Document.defaultProps = {
personTo: {firstName: "", lastName: ""}
};
Source: stackoverflow.com
Related Query
- TypeError when using React: Cannot read property 'firstChild' of undefined
- Cannot read property of undefined when using react hooks
- TypeError: Cannot read property 'firstName' of undefined - using 'props' in react
- React TypeError: Cannot read property 'map' of undefined on passing props
- Uncaught TypeError: Cannot read property 'length' of undefined , using react and redux-form validation
- Faced TypeError: Cannot read property 'counter' of undefined when using Redux in React
- React passing props cannot read property of undefined
- React - Uncaught TypeError: Cannot read property "name' of undefined using Airtable
- Cannot read property 'name' of undefined on React with my props
- React typeerror: Cannot read property of undefined - But I can read other props just fine
- TypeError: Cannot read property 'GlobalWorkerOptions' of undefined in PDFJS.GlobalWorkerOptions.workerSrc when using React js
- TypeError: Cannot read property 'email' of undefined props using Jest and React. Why?
- Redirect with props - React Router - Cannot read property 'props' of undefined
- TypeError: Cannot read property of undefined using React
- React Cannot read property setState of undefined in .then() even using arrow function
- Problem using JS ES6 promises in React - TypeError: Cannot read property 'then' of undefined
- TypeError: Cannot read property 'title' and map of undefined React Hooks props problem
- React Facebook Login with Enzyme mount - TypeError cannot read property 'parentNode' of undefined
- Inventory Management system using React TypeError: Cannot read property 'map' of undefined
- Using React Redux Loading Bar : Cannot read property 'default' of undefined
- Cannot read property of undefined using componentWillMount in React with Meteor
- How to fix error cannot read property of undefined using react and typescript?
- TypeError: Cannot read property 'method' of undefined using react router (express)
- Cannot read property 'push' of undefined when using protected route in React
- Cannot read property 'map' of undefined in react while passing through props
- React : TypeError : Cannot read property 'fullname' of undefined
- getting the error Cannot read property 'value' of undefined in react while using arrow function
- TypeError: Cannot read property 'image' of undefined while using react router
- React js and apexcharts Unhandled Rejection TypeError Cannot read property 'length' of undefined
- TypeError: Cannot read property 'location' of undefined react error on using 'window'
More Query from same tag
- Using getState in redux-thunk async action
- Prevent onmouseout from firing on child elements
- How to add a "show more" button to each card on React?
- Request failed with status code 401 - React Unit-Testing
- What is best way in javascript to stop throwing "ResizeObserver loop limit exceeded" error avoid exceeding Sentry quota?
- React access props from inside a javascript event listener
- Issue using render props to create toggle button to show/hide child components
- Use fs module in React.js,node.js, webpack, babel,express
- React JSON problem, iterate for a each key of a JSON Object
- Semantic UI React transition issue on multiple divs
- React: trigger onChange and update select element if input value is changed by state
- How can componentDidMount wait for a value from redux store before executing (without using setTimeout)?
- Ant Design range picker disable array of dates
- Use Microsoft Graph Toolkit with msal-browser
- Storybook default selection control value
- React.js material-ui: How to format Textfield as an amount
- Programmatically open Tooltip in Material-UI
- React + Typescript: Expecting children component with specific props
- How do I cancel this axios request
- Content Security Policy issue node js react
- React router: reset focus on route change (accessibility)
- How do I lower a variable by a certain value every second in react
- A proper way to make React app Multilingual
- How to overwrite Material UI classes via parameters
- Apollo is mixing results from a GraphQL Query in React
- React props not working for me I am designing a card
- I am getting this error like index.js:1 Warning: Maximum update depth exceeded
- How to fix always float timepicker even scroll?
- HTML to jsx converter not preserving css units
- Responsive layout using Flexbox and Tachyons