Jay
Jay

Reputation: 67

Getting Component State

Lets say I have component named Vault

   class Vault Component{

    State : { animal: dog,color:blue} 
}

Lets say I have button in component named App with a button

class App Component{

State: { animal:null,color:null}

}
<div onCLick = {goGetVaultData()} className="button">Press Me</div>

Question is how does goGetVaultData function look to extract state from a diffrent component

goGetVaultData(){

// what do I look like ?

}

Upvotes: 0

Views: 29

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 84982

If you want data from a parent component, have the parent pass it down as a prop, then access it using this.props.

If you want data from a child component, pass a function as a prop to the child. The child calls that function, and when they do you call this.setState to save the value, and access it later using this.state

If you want data from a sibling component, move the state up to whatever component is the common ancestor of the two components. That common ancestor passes props down to both components.

Upvotes: 1

Related Questions