what is a backing instance in react ? - javascript

Although the term backing instance is referred in react docs a lot, I couldn't get what it means. From react docs :
To interact with the browser, you'll need a reference to a DOM node.
You can attach a ref to any element, which allows you to reference the
backing instance of the component. This is useful if you need to
invoke imperative functions on the component, or want to access the
underlying DOM nodes.
link
This simplified component API is intended for components that are pure
functions of their props. These components must not retain internal
state, do not have backing instances, and do not have the component
lifecycle methods.
link
Because stateless functions don't have a backing instance, you can't
attach a ref to a stateless function component. Normally this isn't an
issue, since stateless functions do not provide an imperative API.
Without an imperative API, there isn't much you could do with an
instance anyway. However, if a user wants to find the DOM node of a
stateless function component, they must wrap the component in a
stateful component (eg. ES6 class component) and attach the ref to the
stateful wrapper component.

A backing instance is the object in memory which represents the node. This is where things like the state are usually stored.
So if you have a stateless component you won't have a backing instance because it is well stateless.
A few points regarding stateless components:
No lifecycle methods
No reference

Related

Understanding the props concept in React

While I'm learning React, it's always said that you should keep your state in the parent component and pass them as props to its children.
But in the real world, whenever I start building a react app, I end up passing data from a child component to its parent.
For example, if I have to implement a form somewhere in my react app, I create an extra component for it (for example, FormComponent) and import it in my App component.
But now, I have to pass the form data form the FormComponent to the App component. In other words, from the child component (FormComponent) to the parent (App component).
And this was just an example. Usually, I always end up in situations where I have to pass data from child to parent rather than the other way around.
Do I misunderstand the concept of passing props completely?
If we stay with the above example, is my approach, right?
You can't pass data from child to parent as in React, The Data Flows Down.
So usually you pass callbacks from parent to its children, but the state remains in the parent.
This is commonly called a “top-down” or “unidirectional” data flow. Any state is always owned by some specific component, and any data or UI derived from that state can only affect components “below” them in the tree.
If you in a situation where the code becomes unreadable, unmaintainable, you should consider Lifting the State Up.
Often, several components need to reflect the same changing data. We recommend lifting the shared state up to their closest common ancestor.
Aside from it, a common anti-pattern is Prop Drilling, Context API, and many state libraries like Redux, MobX, and Recoil trying to solve such problems with some extra features.
1- In your case you can pass a function ( Example: handleSubmit() ) through props from parent to child.
And so when this function is called the child's data would be hundled from the parent.
you can use this doc page to inspire your code.
2- Otherwise you can use react redux, and then you can hundle all data at any component in your project using one global state called redux store.
want to learn more about react redux click here.
3- Have a nice day ^_^ .

Using refs in React in stateful component

The question is aligned more towards the performance or best practices of implementation.
As per React Docs, refs, which are part of React16 can only be used in class-based(stateful) components. As stateless components do not have an instance or state we cannot have refs in the stateless component.
What is the tradeoff of changing the stateless component to stateful or class-based component to use refs? Is it a recommended approach OR if it's only about refs we can use the old native approach of document refs or Jquery of getting an element reference.
Will changing of stateless to stateful component just for using refs and not any of the lifecycle methods are considered as best practice?
As far as I'm aware there is no real tradeoff when converting a stateless component to a stateful component, performance-wise at least (the following article outlines some findings with regards to this). Although you could go ahead an retrieve the DOM element by using document.getElementId or some other native solution, it is generally better to use refs since it's more inline with React's way of doing things (a more detailed Stack Overflow response and thread discussing this you can find here.
In case you are using refs to get an reference to DOM element when some kind of event is triggered, you could also retrieve the DOM node from the event itself without using any refs.
There is nothing forcing you to implement the life cycle hooks, even if you would convert your component to a class based one. Assuming the performance difference is minor it would be appropriate to convert a stateless functional component to a class based stateful component if you have an obvious use case where you want to use ref, although in most cases you could probably get away with using event handlers instead.
If you don't want to convert you functional component to a class based one, you could also use the useRef hook, which would allow you to use refs in your functional component without having to convert to a class based component (since React 16.8).

What is the best approch to send data to its descendant in REACT

FOr example I have one parent component ( i'll call it animal)
and several child components (dog, cat, horse) and child's child components (Collie, English Foxhound, Fox Terrier, German Shepherd Dog). If i wanna send a function from animal to Collie, i have to send this function to Dog component and from Dog to Collie and only after 2 props i will be able to use this function in Collie component. Is there any better approch to send a function from animal directly to Collie ?
As mentioned by others on top the new Context API that was introduced in the new versions of react maybe different in react versions and still not recommended for just avoid passing props a fewer levels down in the component tree.
The component composition is a better option if you don't want to use redux which maybe complex for less usage like this example.
https://reactjs.org/docs/context.html#before-you-use-context
You basically create your parent component in this case <Animal/> to expected children nodes, <Dog />as well to expect children then you can later on pass it <Collie /> with the props.
Use the Context API.
Read more at the official docs : https://reactjs.org/docs/context.html
Basically, you define a context value and pass it with a Provider, which can be accessed by any node within the child tree.
Do note that the Context API is different for different versions of React and may change in future. You may also use Redux and react-redux for state management which would be internally using the context api.
You can use React Context API which is now official. More information as to how to use this can be found here - https://hackernoon.com/how-to-use-the-new-react-context-api-fce011e7d87
I would recommend to take a look at React "Component Composition" pattern (docs: https://reactjs.org/docs/composition-vs-inheritance.html). It is pretty straightforward and doesn't have a reusability issue that sometimes might occur in with React Context.
Ta-ta

Correct way to have a global variable in react

I'm developing a react.js project and before the main component is rendered, I call a function that returns an object that all components should be able to access. What is the correct way of doing this in react? Currently, I'm just passing it as a prop to the main component and then I suppose I should have to remember to pass it as a prop to all other components. Is there an easier or better way of doing this?
It seems like you are doing something like Redux. Passing the object as props should be okay. You could make a higher-order component that wraps your components and adds access to that global object via props. This is similar to Redux's connect.
As the expectation in React is application-wide concerns ,like a flux/redux/apollo store, are kept in a root provider component’s context and then accessed elsewhere in the component tree via a Higher Order Component or render props. This provides relief from globals and circular dependencies, and makes testing those components easier.
However, if you have non-component code that will need access to configuration values, you may need to use config global and writing components in a way that accepts config values from props.
see: https://github.com/lorenwest/node-config

How to test top-level React component managing state?

So I have shallow render tests for all my Components that use just props. Some of them are Stateless functional like FB defines them, some of them are standard extended Components but without state.
Shallow is just great for these components. Very fast and I found a nice way to test that callbacks are passed properly as well without re-rendering.
Anyways - since I had to use rather big parent component that controls state and sets props for child (tested) components - now I have to test the parent component.
Note that this is part of legacy Rails app and currently I cannot use Redux. But I'm considering converting all parent component state manipulating methods (aka addTodo) to pure functions that will accept state as an argument etc.
What's the best practice to test this "parent" component:
Use shallow render again and just make sure that top-level methods (aka addTodo) are passed correctly to child components. Then test if the top-level methods return correct new state - they have to be pure because it seems I cannot access state when using shallow.
Use shallow render like in #1 but to test top-level methods use callback methods to actually call them, then rerender and test if the new "tree" contains new todo etc.
Resort back to DOM and Simulate for this component.
...
Thanks.

Categories

Resources