0

Some thing wrong with my code:

1 in this case header not displaying

import React from 'react';
import {Link} from 'react-router';

import CurrentUser from './../services/current-user.js';
import HeaderWithoutLogin from './header-without-login.js';
import HeaderWithLogin from './header-with-login.js';

let Header = React.createClass({
    render: () => {
        var Child = CurrentUser.isLoggedIn()
            ? HeaderWithLogin
            : HeaderWithoutLogin;
        return <div>{Child}</div>;
    }
});

export default Header;

2 in this case I got an error: "Cannot read property 'state' of undefined"

import React from 'react';
import {Link} from 'react-router';

import CurrentUser from './../services/current-user.js';
import HeaderWithoutLogin from './header-without-login.js';
import HeaderWithLogin from './header-with-login.js';

let Header = React.createClass({
    render: () => {
        if (CurrentUser.isLoggedIn()) {
            return <HeaderWithLogin/>;
        }
        return <HeaderWithoutLogin/>;
    }
});

export default Header;

How I need use my logic? I mean return and render different views regarding the statement

7
  • Can you elaborate on what is causing the state error, from what component the error stems and so on or post each components source code. Commented Sep 9, 2015 at 20:33
  • @limelights createRouter.js:275 => Uncaught TypeError: Cannot read property 'state' of undefined it becasue this is undefined in the HeaderWithLogin, but why? Commented Sep 9, 2015 at 21:03
  • Can you post each components source, please? Commented Sep 9, 2015 at 21:09
  • jsfiddle.net/paveleremin/hzsnoh2h - Header.js jsfiddle.net/paveleremin/zpe8hfyy - HeaderWithLogin.js Commented Sep 9, 2015 at 21:24
  • It must be an error higher up in the hierarchy, can you post full stacktrace, please? Commented Sep 9, 2015 at 21:28

1 Answer 1

1

Your render method within HeaderWithLogin is not correct.

render: () => {}

will try to bind this to something totally different from what you're expecting, which will produce undefined.

Change to this and it'll work.

render() {}

or ES5 equivalent

render: function () {}
Sign up to request clarification or add additional context in comments.

1 Comment

can you help me with other more simple question: stackoverflow.com/questions/32490144/… ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.