I'm making an app with react native, actually I have the screens with classes :
class registerScreen extends Component {
But now I'll like to use "useEffect" hook but see I cannot with classes so I have to migrate them to functions...something like...
const registerScreen = () => {
which is the correct or best practice method? What about the constructor and componentDidMount(), etc...
In the other hand it's possible to use the "useEffect" hook on a screen made with a class? How?
Thanks
React Native Official documents says if you're going to develop a new react native app use functional component and hooks instead using class component.If you already developed your app using class components it's okay,
useEffect() is same as like compnentDidMount(). So In there you call axios or fetch request, load asnyc storage data etc.
You can't use useEffect() in your class components you can use componentWillMount(), componentDidUpdate(), etc.
please refer following official document for more info,
https://reactnative.dev/docs/getting-started
Related
I know Ember has a logger, but I wanted to create my own for learning purposes. I have a service called logger, and I want to be able to use this service everywhere. I have no problem injecting this service into components, controllers, and etc... I cannot figure out how to inject this service into a Utility I created without passing it through the create function. I don't want to have to pass my logger everywhere I create the utility. When I try to inject it into the object it complains about not being in a container. What's the best way to do this?
Okay, its important to understand what Ember.inject.service actually does! Its like a shorter version for this:
myService: Ember.computed({
get() {
return Ember.getOwner(this).lookup('service:myService);
}
}),
So what is this getOwner? It gives you the owner of an Object. Most of your objects like models, controllers, components, views and so on are created by the Dependency Injection (DI) container. For a class to be available on the DI container it needs to be registered.
Your default classes like controllers, routes, views are automatically registered by the Resolver. After registration you can inject them into other classes automatically when they are created by the container. Also into all instances created by the container the owner is injected.
Because the container itself is private, these public APIs are on the Application. getOwner also returns the application.
If you want to manually lookup an instance on the container you can use lookup.
For your utility class you probably use a normal .create() to get the object. This of course will not automatically couple it to your application, so the owner is not available. Also automatic injection will not work.
You can manually inject the owner with the ownerInjection:
myClass.create(Ember.getOwner(this).ownerInjection(), {...});
Then Ember.inject.service will work because getOwner will return the injected owner.
The other thing you could do is to register your utility objects on the container and then look them up. Then the owner is automatically injected.
Not sure which Ember version initiated this pattern but the Ember documentation contains the answer to that question starting from v4.3:
import { inject as service } from '#ember/service';
import { getOwner, setOwner } from '#ember/application';
class Item {
#service('shopping-cart') cart;
constructor(context) {
setOwner(this, getOwner(context));
}
function addToCart() {
this.cart.add(this);
}
}
// On any framework object...
let item = new Item(this);
item.addToCart();
I hit a similar problem a little while back.
The utilities are of type Ember.Object.
So, all you have to do is inject the service into the Ember.Object class as a property.
Like this:
Ember.Object.reopen({
testService:Ember.inject.service('testService')
});
Et Voila!Now you can literally use your service anywhere
I want to create a LinkedinApi class, containing functions which execute web service requests to Linkedin servers. These functions need to be accessible from anywhere in the Play app code. The easiest way I've written such API classes in the past, was to have them declared as an object. Then LinkedinApi.myPublicFunction() is available from anywhere.
The problem is that I don't see how I can declare my LinkedinApi class as an object. It would use Play 2.4's web services, and this is done by adding #Inject()(ws: WSClient) to the class declaration. Something like object LinkedinApi #Inject()(ws: WSClient) extends Controller.
The problem is that the line above doesn't compile. It seems that #Inject can only be used with class declarations, not with object.
So how can I create application-wide API functions which perform web service calls?
I think you should define your LinkedinApi as a service and inject it where needed:
#Singleton
class LinkedinApi #Inject()(ws: WSClient) {
//...
//linkedin stuff
//...
}
and inject as:
#Singleton
class SomeController #Inject()(linkedinApi:LinkedinApi) {
//...
}
Trying to Register the dependency that need to be passed to the Controller Methods as an Interface and after doing some research the below needs to done but after setting it Sitecore throws this errpr
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container.Kernel));
container.Register(Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient().Configure( x => x.Named(x.Implementation.FullName)));
Code Snippet in the controller is
public ActionResult Footer(ISomeFactory someFactory) {}
I am using Glass Mapeer and Castle Windsor for IOC.
You need to tell Castle how to resolve SitecoreController which is not defined in your assembly. Try this:
container.Register(
Classes.FromThisAssembly()
.BasedOn<IController>()
.LifestyleTransient()
.Configure(x => x.Named(x.Implementation.FullName)),
Component.For<SitecoreController>()
.ImplementedBy<SitecoreController>()
.LifestylePerWebRequest()
.DependsOn(new {databaseName = Sitecore.Context.Database})
);
EDIT: Based on comment you can expand this registration to something like this. You may need to review Lifestyle settings for each object, and depending on your Controller's constructors you may need to add in additional implementations.
container.Register(
Classes.FromThisAssembly().BasedOn<IController>().LifestyleTransient(),
Component.For<ISitecoreContext>().ImplementedBy<SitecoreContext>().LifestylePerWebRequest(),
Component.For<ISitecoreService>().ImplementedBy<SitecoreService>(),
Component.For<IGlassHtml>().ImplementedBy<GlassHtml>().LifestylePerWebRequest(),
Component.For<SitecoreController>()
.ImplementedBy<SitecoreController>()
.LifestylePerWebRequest()
.DependsOn(new {databaseName = Sitecore.Context.Database})
);
Ok Lesson Learned. First of all when you are working with Sitecore MVC, Glass Mapper and Solr then don't mix their Windsor containers. I started using the Windsor Container from GM and added the registration code in GlassMapperScCustom.CastleConfig(IWindsorContainer container) to register my components. This caused the SitecoreController to get unregistered and threw the error "No component for supporting the service Sitecore.Mvc.Controllers.SitecoreController was found". When I implemented the solution suggested by Matt Gartman then it worked. But I didn't like this and wanted a more cleaner appraoch. So here are my suggestions.
Create a separate Windsor Container and register your components. Do it the Global.ascx.cs Application_Start()
Keep the Glass Mapper and Solr Container intact. Don't use their containers.
You can now use Controller Constructor DI with this.
If you are trying to do Method DI then that is separate thing and don't assume it to work with Constructor DI. Lesson learned the hard way.
Hope this discussion will help someone who is trying to do all this with these technologies.
We use GWT 2.3.0 for our web applications.
We have started to use gwtquery for some of our features.
I would like to know if it is possible to call a jquery function within a js file from gwtquery.
gwtquery aka gQuery is a completely re-written implementation of the jquery for java.
One of the goal of gQuery is to have most of the features of jquery (css selectors, dom manipulation, effects, promises, ajax, etc) but without having to import the external jquery.js library, benefiting of all goodness of gwt (optimization, performance, dead code removal etc.).
In consequence, gQuery and jQuery cannot share plugins, so if you are using jquery.js in your app because you are using a jquery-plugin you still have to import jquery in your project.
In summary, if you wanted to use the syntax of the jquery but in gwt, you dont need to import jquery not call external js methods from java.
import static com.google.gwt.query.client.GQuery.*;
public void onModuleLoad() {
//add a click handler on the button
$("button").click(new Function(){
public void f() {
//display the text with effects and animate its background color
$("#text").as(Effects)
.clipDown()
.animate("backgroundColor: 'yellow'", 500)
.delay(1000)
.animate("backgroundColor: '#fff'", 1500);
}
});
}
Otherwise, if you don't use gquery and want to import jquery in your page, in order to call some methods from gwt, you must write jsni methods:
native void enhanceMyButton() /*-{
$("button").click(function() {
//display the text with effects and animate its background color
$("#text").as(Effects)
.clipDown()
.animate("backgroundColor: 'yellow'", 500)
.delay(1000)
.animate("backgroundColor: '#fff'", 1500);
});
}-*/;
Finally, in gwtquery, we are working on exposing gquery methods to integrate pure jquery code. This work is being done on a module we have called jsQuery, and the main goals are: that designers could add jquery code in html or ui.xml without importing the external jquery.js, and it can be a fast way to port a jquery plugin to gquery.
FYI: I posted here some of the benefits of using gquery as a complement of gwt
What is the best thing to do to avoid name collision between development/production environment and testing environment for ember based application.
Usually, every Ember application has a namespace :
window.MyApp = Em.Application.create();
in production, I run some initialization functions by redefining the ready property of Em.Application
window.MyApp = Em.Application.create(
ready: function() {
// create some objects
}
);
But, in my test enviroment, I don't want to run these initialization functions because I create objects myself. Using the same definition of MyApp causes a collision because objects I create insert the same element in the DOM. So how can I use the same namespace MyApp without the ready function when I test my application ? I use jasmine to test the application.
I keep the glue code which instantiates my controllers, creates and setups views out of the Application#ready function.
Take pangratz/ember.js-dashboard for an example: core.js holds just the Namespace definitiion, whereas controllers.js, views.js and so on define my classes. The glue code which instantiates the controllers, creates the views and setups the bindings is defined in main.js. The main.js is then used among the others in the 'real' application in the index.html.
I use interline/ember-skeleton for the basic application layout, which itself uses QUnit for testing. But this should be applicable for Jasmine too.