formatjs icon indicating copy to clipboard operation
formatjs copied to clipboard

Wrapped component by injectIntl has state always to null

Open manuelmazzuola opened this issue 11 years ago • 14 comments

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.

manuelmazzuola avatar Nov 09 '15 17:11 manuelmazzuola

Use:

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

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

this.refs.child.getWrappedInstance().state

ericf avatar Nov 09 '15 18:11 ericf

Thank you!

manuelmazzuola avatar Nov 09 '15 18:11 manuelmazzuola

@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

manuelmazzuola avatar Nov 10 '15 09:11 manuelmazzuola

It does on 2.0.0-beta-1

ericf avatar Nov 10 '15 16:11 ericf

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

manuelmazzuola avatar Nov 10 '15 17:11 manuelmazzuola

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 avatar Nov 10 '15 19:11 ericf

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

roderickhsiao avatar Dec 09 '15 06:12 roderickhsiao

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

ericf avatar Dec 09 '15 19:12 ericf

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

bradbirnbaum avatar Dec 13 '15 01:12 bradbirnbaum

@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?

kumarharsh avatar Jan 10 '16 18:01 kumarharsh

@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?

ericf avatar Jan 11 '16 05:01 ericf

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()

kumarharsh avatar Jan 11 '16 14:01 kumarharsh

@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.

ericf avatar Jan 11 '16 17:01 ericf

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

kumarharsh avatar Jan 12 '16 05:01 kumarharsh