As part of my React app, arrow function ContainerCookiesPolicy
calls arrow function LogInTitleTest
(and some others).
export default class MyClass extends React.Component {
constructor(props) {
super(props);
this.state = {};
}
ContainerCookiesPolicy = () => {
if (window.localStorage.getItem('CookiesAgreed') !== 'true') {
return (
<div className='ContainerCookiesPolicy'>
<this.LogInTitleTest/>
{/*Some other functions*/}
</div>
)
} else {
return null;
}
}
LogInTitleTest = () => {
return (
<span className='Log_in_title_text'>{'Text'}</span>
);
}
render() {
<this.ContainerCookiesPolicy/>
{/*Some other functions*/}
}
}
If I execute this code, this is, function ContainerCookiesPolicy
calls function LogInTitleTest
, React will not render anything at all (like it does when your function/object returns null
) and will give the error Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
. However, if instead I copy and paste the content of LogInTitleTest
inside ContainerCookiesPolicy
like this:
ContainerCookiesPolicy = () => {
if (window.localStorage.getItem('CookiesAgreed') !== 'true') {
return (
<div className='ContainerCookiesPolicy'>
<span className='Log_in_title_text'>{'Text'}</span>
{/*Some other functions*/}
</div>
)
} else {
return null;
}
}
it will actually render the entire page with the word Text
on it.
Further considerations:
- If my function
LogInTitleTest
contains<div>
instead, it will not render the page either; if the<div>
are contained on functionContainerCookiesPolicy
, it will render. Same thing if instead of{'Text'}
I just haveText
.
LogInTitleTest = () => {
return (
<div className="Log_in_title_container">
<span className='Log_in_title_text'>{'Text'}</span>
</div>
);
}
- If I change function
LogInTitleTest
to returnnull
, the situation will remain exactly the same:
LogInTitleTest = () => {
return null;
}
I'm using AWS Amplify to render the page. On WebStorm on localhost, I did not observe this weird behaviour.
I tested the original function
LogInTitleTest
on another portion of code and it was working as expected. Hence the issue must be withContainerCookiesPolicy
.
Any idea why this is happening? Any help would be appreciated!
Thanks in advance