Hi everybody, we're starting to dive into creating react-native apps using our existing codebase and now I ran into the different approach how and when actual component instances are created.
I've read the docs regarding elements, components and instances:
So I think I have a good understanding why porting our existing approach is not straight forward. So this is the "problem" demonstrated as an abstract example:
Usually we use factories for instance creation on an abstract layer und implement views only based on an unified interface. So this is an example interface for an view component:
interface IViewComponent {
public function setValue( value : String ) : Void;
}
This is my react native component (at least I thought it is necessary to extend ReactComponent?):
class ViewComponent extends ReactComponent implements IViewComponent {
private var _value : String = "";
public function new() {
super();
}
public function setValue( value : String ) : Void {
_value = value;
}
override public function render()
{
var value : String = _value;
return jsx('<View><Text>{value}</Text></View>');
}
}
This is a factory I tried to create for creating a react native component:
class ViewComponentFactory {
public static function create( value : String ) : IViewComponent {
// Here we usually create an instance - but react native creates only an element :/
var viewComponent : IViewComponent = cast jsx( '<ViewComponent/>' );
viewComponent.setValue( value );
return viewComponent;
}
}
And this is how I tried to embed the component within an container:
class Container extends ReactComponent {
public function new() {
super();
}
override public function render()
{
var viewComponent : IViewComponent = ViewComponentFactory.create( "VALUE" );
return jsx('<View>{viewComponent}</View>');
}
}
If you're already familiar with react you will understand, that in my
the call to
will fail with "viewComponent.setValue is not a function", since the value of viewComponent isn't actually an instance of my
class but the ELEMENT that defines the ViewComponent, which will be generated later in reacts rendering logic.
So now I wonder what options I have to work with the same factory APIs as always?
Is there an undocumented way to create the instance immediately? Or an react native equivalent to ReactDOM.render()?
I think I might work with the ref-callback and wrap some calls into asynchronous handling, but I'ld love to fix this behaviour (at least within my factories).
Did you run into that - what would you suggest?