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

Wrapped component by injectIntl has state always to null #210

Closed
manuelmazzuola opened this issue Nov 9, 2015 · 14 comments
Closed

Wrapped component by injectIntl has state always to null #210

manuelmazzuola opened this issue Nov 9, 2015 · 14 comments

Comments

@manuelmazzuola
Copy link

I've encountered an issue while using the formatMessage method.
I've wrapped a component called ChildComponent with injectIntl.

ChildComponent.propTypes = {
    intl: intlShape.isRequired
}

export default injectIntl(ChildComponent)

Then I've used the component into another component called FatherComponent.

<ChildComponent ref="child"></ChildComponent>

The issue is that when accessing to the ChildComponent from the FatherComponent the state is always null, this.refs.child.state is always null.
I'm using the new beta version.

Instead the state is populated correctly if ChildComponent is not wrapper by injectIntl.

@ericf
Copy link
Collaborator

ericf commented Nov 9, 2015

Use:

export default injectIntl(ChildComponent, {withRef: true});

Then you can access it via the following from your FatherComponent:

this.refs.child.getWrappedInstance().state

@ericf ericf closed this as completed Nov 9, 2015
@ericf ericf added the support label Nov 9, 2015
@manuelmazzuola
Copy link
Author

Thank you!

@manuelmazzuola
Copy link
Author

@ericf Doesn't work like you said.

 this.refs.child.getWrappedInstance is not a function

But I can access to state by this way.

this.refs.child.refs.wrappedElement.state

@ericf
Copy link
Collaborator

ericf commented Nov 10, 2015

It does on 2.0.0-beta-1

@manuelmazzuola
Copy link
Author

@ericf But I'm using the 2.0.0-beta-1 version
The version tagged with next on npm.

@ericf
Copy link
Collaborator

ericf commented Nov 10, 2015

So I see an issue, I actually forgot to return the ref, but it's definitely a function: https://github.com/yahoo/react-intl/blob/master/src/inject.js#L37

@ericf ericf reopened this Nov 10, 2015
@ericf ericf added the bug label Nov 10, 2015
@ericf ericf closed this as completed in 0c4ef30 Nov 10, 2015
@roderickhsiao
Copy link
Contributor

@ericf are we publishing this change in 2.0.0-beta-1?

@ericf
Copy link
Collaborator

ericf commented Dec 9, 2015

@roderickhsiao beta-2 since beta-1 was already published. I can hack-in a patch to beta-1 internally though…

@bradbirnbaum
Copy link

@ericf I also need this to work - any thoughts on publishing beta2?

@kumarharsh
Copy link

@ericf Can this {withRef: true} not be the default? I'm assuming (from my usage) that more often than not, I'd end up using withRef: true especially in Forms, etc.

Also, how would one use this with the decorator pattern?

@ericf
Copy link
Collaborator

ericf commented Jan 11, 2016

@kumarharsh it's cannot be true by default because then it would fail when wrapping stateless function components. Why do you find yourself needing a direct ref to form related components?

@kumarharsh
Copy link

Ok, I understand. I was following a pattern wherein on clicking "Submit", I'd get the values from each input via their refs. Some of those inputs can be components in themselves, which may are wrapped in injectIntl()

@ericf
Copy link
Collaborator

ericf commented Jan 11, 2016

@kumarharsh okay. Another approach is using an onChange callback, similar to the primitive <input> React component, and store values in the container component's state.

@kumarharsh
Copy link

Yes, I'm using that now in some places :)

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

5 participants