Aura get boolean from parent to child - salesforce

I am very new to salesforce. The boolean value in the parent component will be updated by clikcing a button. That is done. Now I want to toggle a class in a child component based on the parent boolean value. So my question is how can I always read the parent boolean value from the child?
parent component
<aura:attribute name="isActive" type="Boolean" default="false"/>
child component
<div class="{!v.siActive ? 'bg-red' : ''}">
........
</div>

Have you seen https://developer.salesforce.com/docs/component-library/bundle/aura:valueChange/documentation ?
Pass the parent attribute to child (in parent have something like <c:myChild isActive="{!v.isActive}">
and then in child define a handler function that will run on value change.
Actually there are few patterns for parent-child communication, pick what looks easiest/what's closest to what you already have

Related

Component child with two parents,

im newbie on Ractsjs, and now i made on course on Plural sight, the situation its the fallowing, i have one component(child) who is the son and in one hand i have a one component2(father) thats send the 'props' for the component3(son) use them; and for other hand, this component3(son) wants send a callback function to the other component1(father),
in resthat´s my schema:
Component1 Component3
|with callbaclk | with props
| |
└ Component3 ┘
::The seem child receive 'props' from on component(father1) and this seem child component need send a callback function to the other component(father2).........how i can do it??
thks in advance
pd) i attached my code
https://github.com/jmonteros81/Building_-Applications_React
enter image description here
Callbacks are used to send data from child to parent. Callbacks are just simple functions. So you have to write a callback in your parent component and Call that callback from your Child component from where you want to send data to parent.
In Parent component define a function.
getAndPassValue(value) {
this.setState({ count: value });
}
in render pass this function to your child
<ThirdComponent getAndPassValue={this.getAndPassValue} />
In Child component from where you want to pass data to parent call this function.
<button onClick={() => this.props.passvalue(5)} />
Now pass this count to your first Child component from parent
Now you can access this count in your FirstChild component using this.props.count and you can get 5 value passed from your third component.

How to send user input data from functional child component to parent class component?

I have a functional component which takes user input from an input field and the parent component is a class component. I want to send the user input data from functional component to class component for the validation purposes.. How to accomplish this?
I would recommend you to move onChange handler to parent and pass it with props to your child component. Also you have to store your value in parent component state and pass it to child.
In this way, you child component will represent user data, but parent component will do all the stuff.
Read more about lifting state up on reactjs.org

REACT: how tell parent component about invalid data from child?

I have a child component that renders a list (after processing some complex logic) that is passed to it from parent using props
Problem
If parent passes an invalid list then child renders nothing and parent shows a empty li
I want to check if child has not rendered any thing so that li can be removed.
Introducing new dependency (noticing parent) won't help in reusability.
You shouldn't provide bad data to components - filter them before passing down.
Obvious answer is to not render li externally - render it inside a child.
To preserve reusability pass this <li /> as a 'tag' or 'component' prop to child and render conditionally (decorate content) when defined. It's quite common pattern in react. You can also use render props pattern.

React - Parent Layout Depends on Children Content

I have a situation where the parents class depends on if the children have content.
<div className="parent">
<div className="child1"></div>
<div className="child2"></div>
<div className="child3"></div>
</div>
For example if child1 is the only child that has content the parent class should be "a", if child1 and child2 have content the parent class should be "ab", etc. Each child maintains their own state, meaning a child could begin with content and through state changes have none at the end. I'm looking at using Higher Order Components and references to solve this issue but haven't found a great solution yet. Wondering if there's a build pattern to help with this problem.
My understanding of traditional React would suggest that if a parent component depends on the state of child components, then that state variable should really be maintained by a shared grandparent like so:
<Grandparent> //Maintains State
<Parent> // receives Grandparent state and state manipulation functions as props
<Child 1> // receives Grandparent state and state manipulation functions as props (via Parent)
<Child ...n> // same as child one.
You'll end up passing functions defined as methods on all the way down to so changes triggered on can impact the state variable on and the props are passed down new.
Passing functions and state down and up the tree can get messy, so depending on how many levels you have to your tree, and how much state you are passing around, you could take a look at redux to simplify the flow. Redux acts as a state container outside the React hierarchy, making universal state variables readily accessible at any level of the tree.

When a component can edit properties in react?

I was going through a code and found that
this.props.formVisibility = false
As one cannot update the properties in react's component, the code shouldnt have work but the execution went without any errors.
Now I am wondering, On what conditions one is able to edit props in the component?
Suppose you have parent - child relationship. where parent's state pass as props to child component then whenever you want to update props you need to update parents state not props in child component. once state get updated this will update to props as well in child component hope so it clear the point
refer : https://gist.github.com/sebkouba/a5ac75153ef8d8827b98

Resources