Guaranteeing child component's height is >= sibling child component's - javascript

I'm looking to do this dynamically. I have a parent component which has two children components. Each children is nested inside its own div. Both children can update and, due to their content's nature, change height independently. I want to guarantee child component 1 div always has equal or greater height than child component 2. When it doesn't, I want it to dynamically change to match its sibling. The code:
class Parent extends Component {
render(){
return(
<div className="childComponent1wrapper">
<ChildComponent1/>
</div>
<div className="childComponent2wrapper">
<ChildComponent2/>
</div>
);
}
}
I thought about saving each children component's height in a store and passing the prop to parent component as a prop, but it seems overkill.
I've tried using parent's onComponentDidUpdate() but it doesn't trigger when children update.
How could I achieve this? Thanks

Related

How do you update state in parent component from a child component when an event takes place in the parent component

I have to update state of the parent class component from child functional component only when a click event takes place in the parent component. Right now I've access of the data from child to parent component but when I update the state I get error, maximum depth reached. So I thought it'd be better to update the parent's state from child only when the certain event takes place in the parent component but I'm unable to find any approach. Could you please show some light?
From your description, the best thing to do is to lift state up from the child to the parent so that the information you currently only have in the child is available in the parent (which can provide it to the child as a prop). Then, the child isn't involved in this at all, the event in the parent updates the parent's state, which is standard.
But if you absolutely can't do that:
It's taking you off the beaten path, but you could:
Have the parent pass the child two things:
a means of subscribing to an event from the parent
a function to call to update the parent's state
Have the child subscribe to the event
Have the parent raise the event the child subscribes to when the click occurs
Have the child respond to that event by calling the function from (1)(2) above
While that would work, it's a bit convoluted. So again, if you can avoid it, move the state needed for the update from the child to the parent and keep the entire update in the parent.
Finally I figured out a way to achieve it.
Step 1: Set a boolean for your event taking place in your parent state using useState hook.
// Inside Parent component
const [eventInParent, setEventInParent] = React.useState(false);
Step 2: pass the eventInParent as prop to your child component
// Inside Child component
Step 3: Receive the event status (eventInParent) in your child component in your props and use it as a dependency in useEffect hook
React.useEffect(() => {
if(eventInParent) {
}
}, [eventInParent]);
As you can see every time event will hit in parent component the eventInParent value will change which will trigger the useEffect hook in the child component.

Using the same component in a child and parent in Vue

I have a VueJS project - which in one view there are parent and child components that are both using the same component called deleteModal.
When I trigger the modal from the child (to show it), it triggers both the child and parent modals (except no data is passed to the modal triggered by the parent). As I understand it, this is because I have used the component twice - in the parent and child - example below. To note, it works as expected from the paren
I've researched and tried a few things: setting a key value for each of the components, changing the components ref name among other things. I have also tried using v-show to only show component just before I trigger the parent model however, this solution is not ideal.
How can I only trigger the modal from the child?
Parent Component
<template>
<div>
<childCompt ref="childCompt" />
<deleteModal
ref="deleteModal"
#deleteTriggerAPI="deleteAPIParent"
/>
</div>
<template>
Child Component - childCompt
<template>
<div>
<deleteModal
ref="deleteModal"
#deleteTriggerAPI="deleteAPIChild"
/>
</div>
<template>
My old answear is not good at all. I personally to show and hide element using jquery in vue. For me right now this is best solution but maybe i don't know some best.
If you want use only vue i using also variable passing to child from parent which will support visable of your modal.
We pass variable with ":" and register event with "#".
<template>
<childComponent :isModalOpen="isModalOpen" #onModalClose="isModalOpen=false">
<template>
export default {
name:"parent",
data: () => {
isModalOpen: false
}
}
In child we catch this by using props. We need to define type of varialbe we pass. Different between props and data is that in props we cannot change value in child component.
export default {
name: "child",
props: {
isModalOpen: Boolean
}
}
And you can use this variable to show or hide modal. Also in child component we can create button to close modal and we emit event to parent in order to change variable value.
To do this we using this.$emit('eventName');
More information right here: https://forum.vuejs.org/t/passing-data-back-to-parent/1201
You could try globally defining the component,
ie, in main.js
Vue.component('deleteModal',deleteModal)

React: moving to a particular child component of my react app

I have a callback function which receives an argument from child component(here CardTiles) and it is processed and passed as props to another child component. The output is displayed correctly but I have to move my page to that particular section(the second child component-OutputWindow). How can i trigger an anchor tag from my 1st child component? or would refs work here?
Child component 1:
<CardTiles parentCallback={handleCallback}/>
Child component 2:
<OutputWindow name={place}/>
or would refs work here
Yes. Have a ref in the parent, put it on the child (ref={theRef}), have the child forward that ref to the outermost (probably) HTML element it renders, and then in the callback function have the code use theRef.current?.scrollIntoView()¹ to scroll that element into view.
More
Refs
Forwarding refs
scrollIntoView
¹ That's using optional chaining, which is fairly new. If your project isn't set up for it, you can use if (theRef.current) { theRef.current.scrollIntoView(); } instead.

React: Any way to get a list of props.children for a specific type of child only? Without iterating through all children to manually create?

Is there any way for a parent component to have a list of all child components of a certain type without manually iterating through every single child in props.children to test if they're of that type? The example below shows the question. The Parent component wants a list of all Child type components. As it stands, I have to iterate over all the props.children (and recursively through all their children) to check if any are of the Child component type. But this could be too costly. Is there some special code that can be placed within the Child and Parent component to signify that each Child component should also be placed in a list within another prop of the Parent component and not just props.children? That way I wouldn't have to iterate over every single component in props.children to find them.
This issue:
function Parent(props){
/*I wish to have a list of all the components of type <Child>
within props.children before making it to the return function.
I would need to iterate over all props.children and their
children as well, and check if its a <Child> component.
I would weed out all other components/elements to only have a
list of all <Child> components
*/
return(
{props.children}
)
}
function Child(props){
return(
{props.children}
)
}
function App(){
return(
<Parent>
<div>Just a div, would be weeded out</div>
<Child>special component that Im looking for</Child>
<div> //this div would be weeded out,
//but the <Child> component in it must be found!
<Child>special component that Im looking for</Child>
</div>
</Parent>
)
}
What I desire
function Parent(props){
//somehow there is special coding within the
//Parent and Child components that this prop
//(listOfChildTypeComponents) gets created so
//I don't have to manually iterate over all
//props.children to create it.
const allChildComponents = props.listOfChildTypeComponents
return(
{props.children}
)
}

How can my child component update my parent component in React JS?

My parent component is a list of tabs. Clicking on tab leads me to Child #1 or Child #2. I can also route to Child #1 or Child #2 by URL. In this case, I check the path to know which tab to select programatically.
My problem comes when I redirect from one child to another. My parent is oblivious to the fact that a change has occurred - none of the initializing functions in that component get called, since it is already rendered. Therefore the tab that was originally selected (Child #1) - remains selected even though I am now viewing Child #2.
The tab that is selected is being stored in the parent's state - is there any way for me to update the parent's state from the child component?
Pass a function down to the child(ren) from the parent that modifies the state accordingly. A very simple version could be something like:
class Parent {
setActive(activeId) {
this.setState({active: activeId});
}
render() {
return (
<Child setActive={this.setActive}/>
);
}
}

Categories