I'm taking singlePost data from Redux and making it into an array using Object.keys.
However, when rendering is in progress, singlePost is received late, so when I try to console.log, null is recorded at first and then the correct data comes in.
So this error is printed.
Cannot convert undefined or null to object
this is my code
const Explain = ({navigation, route}) => {
const {singlePost} = useSelector((state) => state.post);
console.log("singlePost:",singlePost);
// singlePost: null first recorded
// singlePost:
// singlePost = {
// User: {
// id: 3,
// nickname: "bill",
// },
// content1: "number1",
// content2: "number2", second recorded
// content3: "bye",
// content4: "empty",
// content5: "empty",
// content6: "empty",
// content7: "empty",
// content8: "number3",
// content9: "empty",
// content10: "empty",
// };
const contentOnly = Object.keys(singlePost)
return (
);
};
export default Explain;
How can I fix this error? Can I use optional chaining in object?
How can I fix my code?