React-Native parent Node change children value - javascript

I am looking for all web but I no found any solution, maybe because I am newer with React-Native universe.
Well, my question is about Node Parent change value of Children.
My situation is, I have a data controller and this data controller listen data from events and when the events happen it need update a component like Slider.
the render need be.
<DataListener url={this.state.service_url}>
<Slider style={this.styles.slider_component}/>
</DataListener>
I need understand the cycle to get a default Slider with my Class DataListener. Is it with props in constructor? How can I get the slider inside DataListener and change this value like this.default_slider.value = 0.4
Thanks Advance
Victor C Tavernari

You should pass down the value as a prop and then your slider component can deal with it appropriately. Of course DataListener has to get the value in the first place and as you have detailed that let's assume it is in state (when you update state (via this.setState(newState)) it will cause a re-render of the DataListener which will in turn re-render the Slider with the new property):
<DataListener url={this.state.service_url}>
<Slider style={this.styles.slider_component} value={this.state.value}/>
</DataListener>
the data controller event you refer to would then be something like
onEvent(e) {
this.setState({value: e.value});
}

Related

Vue.js - Clearing Child Form Fields from Parent Component

I have a child sidebar component with some dropdowns. Simplified example:
You select a status here and it re renders the table/dashboard view in the parent component. That is handled via $emit in the child component like so:
selectedStatus: function(status) {
this.$emit('updateStatus', status)
},
No issues there. Now I'd like to be able to clear or reset this input from the parent component. As I said this is a simplified example and there are many dropdowns, a couple datetime selectors, and other inputs to clear.
If I do something like
this.selectedStatus = [] in the parent, it does indeed reset/re render the table/data properly but it still shows the UI piece (ie, the selected status in the dropdown).
If I do something like
this.selectedStatus = [] in the child it does correctly remove/alter the UI piece to where it clears the dropdown.
What is the correct approach here to clear child filters/inputs from the parent?
I recommend you to read this article, it explains all the ways to make a child component rerender (and therefore reset it's state).

React useEffect on dom element

Let's say I have two "buttons" (they're div elements with an onClick attached). Each one changes a product's price inside a div element.
How can I do something when that div was clicked, inside another component ?
Can I somehow put dom element to the dependencies array of an useEffect ?
for example:
useEffect(() => {
// Do stuff
}, [document.getElementById('div-that-shows-price')])
I tried the above, didn't work. I even tried it with .textContent, innerHTML, ....
I'm guessing I can't use ref because again, they're in different components. Or can I use a ref defined in a component from another one ?
I think you have to use useRef hook:
https://fr.reactjs.org/docs/hooks-reference.html#useref
The question is not clear. But from what I understand, why don't you pass price as props to another component? You can put product price in a parent component state and pass it as props (with updatePrice method) in both button components. Or use global state (redux).
I am guessing, what you building is basically counter, in one div you want count aka price to be able to incremented and decremented, and in the other div you would like to see the button click result. Since you're using React, this is achieved by useState() and just passing result prop from parent component div which would have the button to the child component div.

Issues on animating a conditionally rendered component using React-spring

I am new with react-spring. I am having trouble animating a component whenever it unmounts. I have a simple card with an onClick handler that's responsible for conditionally displaying my Overlay component. The animation works fine when mounting (from & enter works), but when closing the overlay, the component just disappears without animation (leave does not work). I suspect it's because of the conditional rendering of the component but I've been struggling for hours trying to find a solution for this one. Any help would be appreciated!
My current code: https://codesandbox.io/s/dry-leftpad-h3vmv
What I'm trying to achieve: https://codesandbox.io/s/048079xzw
P.S. The latter is using mauerwerk's lib. I don't want to use that.
What you were missing is this:
return expand.map(({ item, props, key }) => (
item && <animated.div
// ...etc
When you're controlling the mounting of a single component with useTransition, you need to conditionally render it based on the item being passed. In your case, when it's false it won't render (which will unmount if already mounted) and when it's true it will render (mount if unmounted).
Here's a working sandbox forked from yours: https://codesandbox.io/s/infallible-agnesi-cty5g.
A little more info
The first argument to useTransition is the list you want to transition. That watches for changes and sends back an array mapped with each item, a key and a style object (props) based on whether the item is truthy (entering) or falsy (leaving). So for a transition that mounts/unmounts a single element, conditionally rendering based on the truthiness of the item is key.
Check out the examples again here and you'll see the differences between transitioning a list, a toggle between two elements, and a single item.
For a list, no need to check for the existence of the item because the array changes.
For toggling between two elements, you use the truthiness of item to determine which element to render.
For a single element, item determines whether to render at all. This means it won't mount initially when you default to false, and will make sure you don't render 2 items whenever your isActive value changes.

How to reload a component on a change event in angular

I have angular component, which is loading other components , now in a event change say dropdown item selection, i need to reload just one specific component. Is it Possible to do that?
I looked into some answers in stack overflow itself but mostly are about reloading the whole page.
<dropdown (change)="detectChange($value)" />
<Component-one></Component-one>
In one component i have a 'drop down' with few values, and other component say 'component-one' already loaded with it , when the value in the "dropdown" changes i need to reload "component-one".
It is a general question. First of all you need to understand what you want to achieve in the end.
One way for "reloading" component, pass some data to the component based on the dropdown value, so, it will automatically "reload".
The other way is that you can have url param or query param based on the dropdown value, and on dropdown value change, navigate to the route with new param value, so the component will "reload".

Send an event up multiple parents to trigger a function Angular 5

I have a main-page.componenent.ts like so...
<app-page-component></app-page-component>
and then inside my app-page component I have another nested component like so..
<nested-compoennt></nested-component>
so basically my app looks like this
<app-main-component> // where the function is
<app-page-component>
<nested-component></nested-component> // where the button is
</app-page-component>
</app-main-component>
now I have a button in my nested-component and I want that to trigger a function on the top parent component.. now I know if I was passing an event to the most direct parent I could use an event emitter but Im not sure if that will work in this case.. so my question is how can I 'bubble' up an event so it hits the most parent container and triggers and event?
any help would be appreciated
You have two options.
The first option, you can have each component have an output that broadcasts the event up. This can get unweildly pretty quickly, especially if it is deeply nested.
The second option, you can use a shared service to send and receive messages. Essentially your component that has the button would call a method on your service that broadcasts out the event and your parent component would use the service to subscribe to these events that are broadcast out and act appropriately.
There are lots of cases and little bit hard to understand what your case is.
In my view,
There would be 2 opstions.
grandparent <=> parent <=> children : event emitter
(https://angular.io/guide/component-interaction#!#bidirectional-service)
using service : define service and call children components to set data or make events, and grandparent subscribe the value changes or event subscribe.
I hope this would be helpful :)

Categories

Resources