Passing props down to component which are not children - components

I have to systematize some knowledge.
How can I pass props
ParentComponent
-Child1Component
-Child2Component
-Child2.1.Component
-Child2.1.1.Component
-Child2.1.1.a.Component
-Child3Component
-Child4Component
First issue
How can I pass props from Child3Component to Child4Component ?
form Child3 -to- Parent & from Parent -to- Child4 ?
using context
what else?
What is the bast way to pass props through components which are not connected ?
Second issue
How can I pass prop from Child2Component to Child2.1.1.a.Component so that the intermediate components don’t use that single prop.
I was trying to understand Composition on reactjs.org but I need clearer explanation.

Redux and useContext are the ways you can pass the state to non connected components.
When you are building a small scale application then you can go with useContext. But when you are going to build any large scale application than go for redux. Because redux helps you to keep your code organised and helps to test the code easily.
But never try prop drilling. It is ok if u pass the props by max 2 levels.

Related

Is there a recommended approach to referencing an instantiated class object in React / Nextjs?

Let's say I have a Node.js library that I need to instantiate in my React app.
const awesomeLibrary = new AwesomeLibrary('someID', options);
awesomeLibrary allows me to do things like awesomeLibrary.configure(), awesomeLibrary.specifyCustomProperty() and awesomeLibrary.fireEvent().
I would like to instantiate it once and then call those methods from the instantiated reference (awesomeLibrary) around my application. What is the best way to that?
Would I instantiate it in top-level entry component (e.g. _app.tsx), export it, import it into other components and call the methods? In order to export it, I'd have to instantiate it outside the component, instead of in the lifecycle of the component. Is that a bad practice?
Alternatively, could I relegate the instantiation to a hook? Say useAwesomeLibrary() in which I instantiate and export the methods? But wouldn't doing that create an instance every time the hook is called from different components?
I'm really not sure how to handle this.
Instantiating class before exporting it is a fairly common practice and one of the possible implementations of singleton in JavaScript/React. For example, you can use popular i18next library in this way:
services/i18n.js:
import i18n from 'i18next';
i18n.init({
// pass translations and other config
});
export default i18n;
_app.js:
import i18n from 'services/i18n';
// snip
<I18NextProvider i18n={i18n} /> // consume already instantiated i18n object
The difference between this and instantiating object in a hook is WHEN it will be instantiated. When you do this in hook, it will instantiate every time you mount component with this hook. When in separate file (module), then it will be once per page load.
Is that a bad practice? I don't know, it depends on how you'd use it. There is nothing bad in doing this per se, and even it has its advantages (in the example above we delegated responsibility of creating and configuring i18n service from _app.js, which is very good from Single Responsibility Principle perspective). But keep in mind that codebase bloated with singletons will probably become hard to maintain.

Unreal Engine 4 Blueprints assign by reference

I have an EnemySpawner Blueprint Class that does:
EnemySpawner has a Spline Component named "Path" which the Spawned Enemy must follow. For it, Enemy's Blueprint has another Spline Component named "Enemy Path" that must be initialized by the Enemy Spawner at the moment of the creation, as does in the screenshot above.
But, in my Enemy, if I try to access to "EnemyPath", I get the following error:
Blueprint Runtime Error: Accessed None trying to read property
PathActor from function: 'ExecuteUbergraph_Enemy' from node:
DestroyActor in graph: EventGraph in object: Enemy with description:
Accessed None trying to read property PathActor
So, in other words, Null Pointer Exception.
If, for example, in the Enemy's Blueprint I do (with testing purposes):
The Actor Owner of the Spline Component isn't destroyed.
Even if I try to assign all the Owner actor, not only the Spline Component, it doesn't work either.
I've also tried to print something in screen after the "SpawnActor Enemy" node of EnemySpawner and in the "Event BeginPlay" node of Enemy, and I've checked that obviously the print in EnemySpawner happens first, so the only problem is that the variable isn't really assigned.
The last thing I've tried is to destroy the Spline's Owner right after the "Set" node, directly in EnemySpawner blueprint, by getting it from the Spawned enemy, and to my surprise, it's destroyed!
So, some idea about what's the properly way of doing this?
You can set any variable in a BP actor with an "Expose on Spawn" flag (in the details panel when the variable is selected in the BP editor) which will add a pin for a value to pass into it when using the SpawnActorFromClass node. Make sure you set the variable(s) "Editable" as well.
I think maybe we are doing this wrong.
In straight up C++ you create a class, create the variables, modify the variables by creating a function & passing by ref.
I think that if the Blueprint is a class ( which it is), and if the variable is a variable (which it is), you can't really expect to assign a value to it without calling a function that does it... Then again for all i know maybe the engine does do it for you...

Add renderer in #view_config from configuration?

How do I supply a configured value to a #view_config-decorated function or class?
E.g.
#view_config(route_name='example', renderer=some_config['template.name'])
class MyClass(BaseView):
...
Or
#view_defaults(route_name='example', renderer=some_config['template.name2'])
class MyClass2(BaseView):
...
Or
#view_config(route_name='example', renderer=some_config['template.name3'])
def method3(request):
...
It's very hard to know where to start, as I'm trying to edit a pyramid plugin, which pulls together its config in an includeme function, so it doesn't have anything obvious that I can include, and it's hard to know what's available to the #view_config decorator.
You can add views using declarative configuration (what you are doing now using #view_config or alternatively using imperative configuration by calling config.add_view() method.
In this case, as you need to access the Pyramid registry and settings file, it is easier to do adding the views imperatively.
In your __init__.py you can do:
settings = config.registry.settings
# You need to call config.add_route("foobar") to map view to URL also
config.add_view('views.MyClass', route_name="foobar", renderer=settings['template.name3'])
Then in your views.py:
class MyClass(BaseView):
pass
#view_config() and add_view() arguments are equal.
I thin kyou can also mix view_config and add_view() arguments for the same view, but I am not sure aobut this. Hope this helps.

How to perform a search on several entities with Symfony 2

I need to perform a search on several entities with the same string then order the results.
I've heard/read a little about FOSElasticaBundle, would this bundle be able to do it? It seems (to me) to have almost to much features for this purpose and I'm not sure it could run on a shared server (hostgator).
The other solution I can think of at the moment is doing the search "manually" (by using join and union) but I'm wondering where should I put such a function: in an existing controller, a new one, a new bundle or somewhere else?
I'm worried as well that this manual solution could come to a cost, especially on some non-indexable fields.
You would do custom entity repositories. Check out the docs. Basically this extends the default FindAll, FindOneBy, etc.
You would have a function like so:
class MyEntityRepository extends Doctrine\ORM\EntityRepository {
public function findByCustomRule(){
//this is mapped to your entity (automatically adds the select)
$queryBuilder = $this->createQueryBuilder('someAlias');
$queryBuilder->orderBy('...');
//this is mapped to any entity
$queryBuilder = $this->getEntityManager()->createQueryBuilder();
$queryBuilder->select('...');
//result
$result = $queryBuilder->getQuery()->getResult();
}
}
This class is defined in the doctrine mapping and lives inside the Entity folder.. Check the docs out and you should get a basic idea.

boilerplate js sharing data between modules

I need a way to share information between modules - not only between components in the same module -.
I have common data to share between the different moduleContext.
-I tried using the application context (moduleContext.getParentContext().setSettings() or getSettings(), but each module context has a different moduleContext.getParentContext().
-I also tried creating a singleton object for the application, but even in this case the singleton data of the first module (landing page) are not available for the other modules.
-The third thing I tried is to pass data via notifications (notify / listen), with the same results.
Does anyone knows how to solve this problem?
Marcos
If you want to keep relationship between the module tree, it is necessary that you create your module hierarchy by calling:
parentContext.loadChildContexts(moduleContexts);
When this is done, that method ensures the 'event mediator' of child contexts is set to the exact same 'mediator' instance of the parent context. Then an event occurring at any of the modules will be notified to all the contexts in the same module tress.
It is the same for settings as well. When 'loadChildContexts' method is used, the settings of the parent context are 'copied' to child context settings.
If it still doesn't work for you, can you share your code to me? I may help you to find where the issue is.

Resources