I know using #observer on more small components is good for the performance. Is it also reasonable to use #inject on as many as possible components ? Obviously, in this way it is easier to access the used props than passing through parent components. At the same time, it also becomes difficult to create presentational components. Nearly all components are directly connected to Mobx's stores. But even if we don't use #inject, components may still use #observer.
So my questions are:
How to create presentational components in Mobx? Or maybe there is no need for presentational components in Mobx.
What is the best practice to use #inject ? Or which components are reasonable to used with #inject?
Any suggestions are appreciated.
Question1:
You create presentational components the same way as in redux. Just give these components the props from outside and only use #observer on them.
There is no "need" to use presentational components. Even in Redux there is no need. It just depends on how you can reach your architectural goals/non-functional requirements.
You need to know and decide if you need presentational components without #observer. That could be the case when you for example want to reuse the presentational components in an application, that doesn't use mobx. Then you will run into problems when you have annotated every component with #observer.
Question2:
Use #inject and #observer on the container components and just pass the props down to your presentational components that are only annotated with #observer.
Related
Before react hooks, we called functional components to stateless components.
At that time it was really faster than class components but what about now?
Since we have react hooks, stateless components are no longer precise.
Does the functional components are faster than class components which runs same functions?
e.g.
In functional components,
I can handle state variables by using useState hook.
useEffect can represent componentDidMount, componentWillReceiveProps or some other lifecycle methods in Class component.
... ... ...
We have many other hook functions but which hooks will make my class components faster or lightweight?
Using React hooks will definitely reduce the amount of code you have to write in comparison to class based components.They are much easier to read and debug.
In sense of Performance , in class components :
Cleaning up and applying the effect after every render is task heavy,
and we might right run into issues or bugs.
so overall Hooks is a better option.
source
Both functional and class components in React offer the same functionality. You shouldn't miss anything when using any of the approaches and then you shouldn't differ in rendering performances.
In general, you write less code by using a functional component. This will be more understandable by your colleagues
According to the docs, there is no difference in terms of performance: https://reactjs.org/docs/hooks-faq.html#are-hooks-slow-because-of-creating-functions-in-render
Hooks, as I understand them, are just a way to add state (and lifecycle methods) to function components.
This FAQ answer:
In the longer term, we expect Hooks to be the primary way people write React components.
https://reactjs.org/docs/hooks-faq.html#should-i-use-hooks-classes-or-a-mix-of-both
and this documentation snippet:
Hooks let you split one component into smaller functions based on what pieces are related
https://reactjs.org/docs/hooks-intro.html
confuse me slightly as a React beginner.
It is as if I have to think about my app in terms of hooks rather than in terms of elements and components.
React didn't remove components, but the quote hints that hooks will replace
components as the primary source of code reuse.
Can one still talk about elements and components as primary abstractions in React?
The concept of components is not going away, it's just how they are written that is changing.
The second line you quoted
Hooks let you split one component into smaller functions based on what pieces are related
Is poorly worded in my opinion, and should rather say "Hooks let you split one class component into smaller functional components..."
So instead of having one monolithic class that handles all state and lifecycle logic in methods like componentDidMount, componentDidUpdate, you can split areas of concern and have smaller functional components that only care about things directly related to themselves.
Edit: This snippet from the hooks-intro doc might be helpful:
Hooks don’t replace your knowledge of React concepts. Instead, Hooks provide a more direct API to the React concepts you already know: props, state, context, refs, and lifecycle. As we will show later, Hooks also offer a new powerful way to combine them.
Currently, there are 2 ways of building components in React:
Class Component
Functional Component
You can check them here. The way you build it it's different. In case you approach the class component, that means that you need to work with the prototype of the object.
On the other side, if you choose to go as a functional component, that means that everything you request from React, is invoking a function, and not using the prototype at all. All this chaining from JS can slow down the performance, and that's why the React core team decided to go into pure functional direction: It can perform better, and it makes it even simpler to read it (once you get used to it).
Regarding the second quotation. It is a way to show more benefits of functional programming over classes. They are more flexible and can have a better composition for it.
One last addition: Approaching functional components doesn't mean you can ignore to learn class components. There's still a lot of apps with some legacy code. I would recommend learning both, and whenever you need to create new components, approach the functional component paradigm.
I am curious whether there is any difference when we try to differentiate react components as controlled vs uncontrolled and stateful vs stateless. Are we differentiating the same thing here with different terminologies or is there an actual difference when we distinguish in such a way? I am beginning to suspect there are subtle changes but cannot wrap my head around them. If there are no differences please provide a brief explanation as to why such a reasoning is invalid. If there are please explain the differences no matter how minute they may be.
While in a few cases, these terminologies might mean the same, there is actually a difference between them
controlled vs uncontrolled is basically used to specify whether the view or the state of the component can be controlled from within itself or from outside.
stateless vs statefull is used to describe whether the component contains state or not. A controlled component can also contain a state. In React generally a stateless component is written as a Functional component which is View only component but can also be written as a Class component making use of lifecycle method/or not
Not sure if it's the right website to ask this question but I I'll give it a go.
When building a web app with React and Redux, how should one use the connect() function? Is the goal to only have one container connected to the store at the top that passes the props to it's children or to have multiple containers connected to the store that passes down props to their children?
I've built a small web app that only has one container that is connected to the store and therefore it passes all its props down.
I was wondering what solution was best and if it's the multiple containers connected one, how does one build his app's architecture that way?
Thank you.
Quoting the Redux FAQ at http://redux.js.org/docs/faq/ReactRedux.html#react-multiple-components :
Early Redux documentation advised that you should only have a few connected components near the top of your component tree. However, time and experience has shown that that generally requires a few components to know too much about the data requirements of all their descendants, and forces them to pass down a confusing number of props.
The current suggested best practice is to categorize your components as “presentational” or “container” components, and extract a connected container component wherever it makes sense:
Emphasizing “one container component at the top” in Redux examples was a mistake. Don't take this as a maxim. Try to keep your presentation components separate. Create container components by connecting them when it's convenient. Whenever you feel like you're duplicating code in parent components to provide data for same kinds of children, time to extract a container. Generally as soon as you feel a parent knows too much about “personal” data or actions of its children, time to extract a container.
In fact, benchmarks have shown that more connected components generally leads to better performance than fewer connected components.
In general, try to find a balance between understandable data flow and areas of responsibility with your components.
Tons of examples, describing React and Flux uses global variables: global Dispatcher, global Store and etc. Is there any way to proper inject dependencies to react components? There are some articles on the web describing how to use Dependency Injection component with React, but it based on undocumented weird feature called "Context" with unknown future.
Proper injection, for me, is classic constructor-based injection, without accessing global variables, without accessing static state and other.
It seems that I need to hook into component construction process (place, where new called). Can I do that? If so, how can I do that?
The most straightforward way to do dependency inversion with React is to simply pass any necessary dependencies as properties to the components that need them. Context is simply a way to pass the props down through child components, grandchild components, and so on implicitly without listing the prop on every component that may need it.
You'll find a lot of examples of this technique in the wild, and many make use of context and other niceties. React Redux is a popular one right now; pass a local variable to a top-level <Provider> component as a property, and it wires up the rest for you.
Dependency injection is less straightforward, and you'd probably need some kind of proper DI container or a DI library for JavaScript to do this nicely. Components aren't really instantiated, from the user's standpoint, the way other JS objects are, so doing "at instantiation time" injection of dependencies into components isn't how I'd approach the problem.