nfl / react-helmet

A document head manager for React
MIT License
17.33k stars 659 forks source link

Helmet.rewind() returning undefined #88

Closed abhijeetNmishra closed 8 years ago

abhijeetNmishra commented 8 years ago
const head = Helmet.rewind();

I am getting undefined for above code on server side ?

cwelch5 commented 8 years ago

Before you call rewind, are you rendering a Helmet component with valid parameters? And are there any warnings or errors otherwise? Please refer to the README for various examples to get you started.

sergesemashko commented 8 years ago

I can reproduce the same issue when there is no <Helmet /> render occurred in components on server rendering. Returning a fallback for mapped state on rewind() call would help to avoid the error. If rewind() is wrapped by following code, rewind() doesn't return undefined anymore:

static rewind = () => {
            let mappedState = Component.rewind();
            if (!mappedState) {
                // provide fallback if mappedState is undefined
                mappedState = mapStateOnServer({
                    title: "",
                    baseTag: "",
                    metaTags: "",
                    linkTags: "",
                    scriptTags: ""
                });
            }

            return mappedState;
        }
cwelch5 commented 8 years ago

@sergesemashko Thx for the PR, will take a look soon.

sergesemashko commented 8 years ago

@cwelch5, no problem. Myself and at least two more devs (one from referenced project and the person who submitted this issue) run into this problem. Adding a check in every project that might run into the same issue seems like a big code duplication to me :) I would suggest to prevent it from the package itself, or at least warn users about the check by docs or examples in case of empty state.