How to preserve a component's instance while moving it to another parent component in react?

keep component alive angular
angular preserve component state
routereusestrategy lazy loading
angular router preserve component
how to send data from component to service
how to store data in angular 7

Suppose we have two sibling react components called OldContainer and NewContainer. There is a child component inside OldContainer that contains a <video> tag, and the video is currently playing.

The user can now drag the child component (with the video) and drop it in the NewContainer, and they expect the video to keep playing while it's being dragged and after being dropped.

So the video appears to stick to the mouse position, and when dragged and dropped in the new container, it animates to its new position (again, it doesn't get paused).

How would you implement this? Can we implement this in a pure way (in line with the spirit of pure functions)?

Clarification: I could have used some other element instead of a video tag for explaining this problem. A NumberEasing element would be a better example, since it would require the props and state of the component to be preserved during and after the interaction.

Update 1: Code examples obviously would be nice, but what I'm mainly looking for is just a general description of how you would approach this problem in a "functional" way. How do you keep your view code simple and easy to reason about? Who handles the drag-and-drop gesture? How do you model the data that's fed into the views?

What is it that you want to preserve? Is it Javascript objects that the component holds as state, or is it state in the DOM (like how long a video has played, or text selection in an input box)?

If it's just Javascript objects as state, you're better of moving the source of that state to another service (something like Flux). That way, it doesn't matter if the component gets recreated because it can be recreated with the state that was there before.


The way to keep your view code simple and easy to reason about is to not keep state inside your components. Instead, all data that the component needs should be passed into the component as props. That way, the component is "pure" in that it renders the same output given the same props. That also makes the problem of wanting to reuse a component instance a non-issue, since it doesn't matter when the same input gives the same output.

For drag and drop, I'd suggest looking at:

How you model the data you pass to view components is up to you and the needs of your application. The components shouldn't care, they should just expect to get data passed as props, and to render them. But the popular approach to dealing with this is of course Flux, and there are many libraries that implements Flux in different ways.


Regarding if you have a subtree with hundreds of components that you want to move: I'd still start off by making the state external (pure components), and render that tree in a new place. That means that React will probably recreate that entire subtree, which is fine. I wouldn't deviate from that path unless the performance of it turned out to be horrible (just guessing that it might be horrible isn't enough).

If the performance turned out to be horrible, I would wrap that entire subtree in a component that caches the actual DOM tree and reuses it (if it gets passed the same props). But you should only do this when absolutely needed, since it goes against what React tries to do for you.


About gestures: I'd start out with listening to gesture events in componentDidMount, and in the event callback call setState on the component with the coordinates it should have. And then render the component in render with the coordinates given. React won't recreate the component when you call setState but it will re-render it (and diff the output). If the only thing you changed was the coordinates, it should render fast enough.

If that turns out to be too slow, like if the subtree of that component is huge and it becomes a bottleneck to recreate the subtree of vDOM, I'd reposition the DOM node directly in a RAF-loop outside of Reacts control. And I'd also put a huge comment on why that was needed, because it might seem wierd for some other developer later.

How can i preserve the component state in order to make back , So if I get your question right your problem is that you have a list, click upon an item in that list to get to the detail view (which is an own component) and you  The drive is wrapped in an anti-static bag to protect it from shocks. Finally, somewhere in the box is a little packet of desiccants (typically little silica beads in a perforated bag) to absorb any moisture and keep the container dry during shipment. Padded, electrically protected, and dry is the name of the game.

Create a new variable using const or var. Put the instance of data using rest spread operator, update the necessary data to pass and send the data to the component without mutating the state of component.

Just like:

const data = {
new_data : 'abc'

Preserve Component State, In this lesson, we're taking a look at a powerful Vue.js feature - the keep-alive component - that allows us to preserve a dynamic component's state. This feature​  end component; attribute syn_noprune of CELL : component is true; -- Synplify must somehow recognize that the -- component is a black box for the attribute -- to work. Also, component instances can be associated with the syn_noprune attribute. The syn_noprune attribute works the same on a component instances as with a component declaration.

Take a look at this library : react-reverse-portal

Preserve components between routes · Issue #225 · ReactTraining , Currently, handler components are given a key relative to their route's path, which means that you can't use react's diff algorithm between  If you need to cushion components, you can wrap newspaper outside the antistatic bag. With only 2 items (motherboard and HDD) I don't see where this would be necessary.

Cache components with Angular RouteReuseStrategy, Everytime we navigate between components Angular destroys the previous component We can manage here how to store the RouteHandle. Step 1: Sterilize the jars: Wash the jars, lids and bands in hot, soapy water. Put the jars on a rack in a pot of water. Boil at least 10 minutes; keep in simmering water until ready to fill. Step

Usage with React Router, <Provider store={store}>. <Router>. <Route path="/" component={App} />. </​Router>. </Provider>. ) Now the <App /> component will be rendered if the URL  We will update the Master Component to reflect your changes. Preserve Overrides. When swapping between Instances, you may want to preserve the Overrides you've applied. At the moment, we can only preserve Text Overrides. We won't keep any other Overrides - such as Colors and Effects - when you swap an Instance.

Organizing State, There are a number of community packages that implement various approaches for storing per-component state in a Redux store instead, such as redux-ui,  If a value is specific to only a single component (or perhaps a single child of that component), then it’s safe to keep that value in local state. Takeaway: keep UI state and transitory data (such as form inputs) in local state.

  • <NewContainer container={this.refs.OldContainer} />
  • assuming we have a wrapping component you could always define a ref="OldContainer" (STRING!!) of a mounted component which can be accessed through this.refs["OldContainer"] of the wrapping component. passing this into the props of another component would take this reference I suppose?
  • Actually the containers need not to know about each other. So <NewContainer container={this.refs.OldContainer} /> is irrelevant.
  • why should they know about each other?
  • Accessing the underlying DOM element is only part of the solution. Although I'm looking for a solution that doesn't require the components to directly access and manipulate their underlying DOM element, or at least provides an abstraction layer for that.
  • doesnt make sence cloning the element, because you dont need 2 representations. Going with the instance is perfectly fine imo.
  • I don't suggest cloning the element, I only suggested to use conditionals in the render method and render the components in different places. That will make React throw away and recreate the component. As long as the component itself doesn't own state, that's a better approach.
  • I wrote an update: What I'm mainly looking for is just a general description of how you would approach this problem in a "functional" way. How do you keep your view code simple and easy to reason about? Who handles the drag-and-drop gesture? How do you model the data that's fed into the views?
  • reasoning about a functional approach would require a lot more detail i think. It depends on where your components are and how you can access them and maybe other factors as well. Cloning or recreating a component just to shift it somewhere else adds a very unnatural touch.
  • Recreating it in another place is certainly not unnatural in the React world, it's the idiomatic way to move nodes in React. In an ideal world, components are pure (same input gives same output) so you don't even have to care if the component was recreated or reused by React.