Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDefaultProps overrides properties passed from parent component #107

Closed
borodyadka opened this issue Apr 23, 2015 · 2 comments
Closed

Comments

@borodyadka
Copy link

Hi. There is a problem with passing of messages to children of my top component. Using of getDefaultProps overrides any messages passed from parent. I've using it for default translations, maybe there is another way to do it.

Here I've prepare an example of bug.

If I comment getDefaultProps in my ChildComponent then test passes.

@ericf
Copy link
Collaborator

ericf commented Apr 23, 2015

What behavior were you expecting? A merge of messages?

You could do this:

const ChildComponent = React.createClass({
    mixins: [ReactIntl.IntlMixin],

    getDefaultProps() {
        return {
            messages: Object.assign({}, this.context.message, {
                foo: 'Foo'
            })
        }
    },

    render() {
        return (
            <FormattedMessage message={this.getIntlMessage('foo')} />
        );
    }
});

@borodyadka
Copy link
Author

Thanks for answer. I've expected override of default messages with new.

Your example doesn't work. I've got an error: TypeError: Cannot read property 'messages' of undefined.

Looks like this.context and this.props is undefined in getDefaultProps.

Before I've used a hack like this:

render() {
    this.props.messages = this.props.messages ? this.props.messages : getDefaultMessages();
    // ...
}

But now I've removed it and pass messages property explicitly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants