Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access child state in react?

I have made my own form component with render() method looks like this:

 render() {
    return (
        <form onSubmit={this.onSubmit} ref={(c)=>this._form=c}>
            {this.props.children}
        </form>
    )
}

Notice that children are rendered here as {this.props.children}, so the user can use this component like this:

 <Form onSubmit={this.submit}   >
            <Input  name={"name"} id="name"  labelName="Ime" placeholder="Unesite ime" type="text" >
                 <Validation rule="required" message="Ovo je obavezno polje"/>
            </Input>
            <Input  name={"email"} id="email"  labelName="Email" placeholder="Unesite email adresu" type="text" >
                <Validation rule="required" message="Ovo je obavezno polje"/>
                <Validation rule="email" message="Ovo je nije valjana email adresa"/>
            </Input>
            <button type="submit" value="Pošalji" >Pošalji</button>
       </Form>

I would like to check the state of each Input component (to get its validity) inside onSubmitMethod().

  checkValidity() {
    var sefl = this;
    this.props.children.map((child) => {
        if (child.type.name === "Input") {

//How to get state of child here
        }
    });
}
onSubmit(event) {
    event.preventDefault();
    var obj = serialize(this._form, { hash: true });
    const validityOfForm = true; //hardcoded for now
    this.checkValidity();
    this.props.onSubmit(obj, validityOfForm);

}
like image 408
Vlado Pandžić Avatar asked Oct 21 '16 09:10

Vlado Pandžić


People also ask

How do you access the child state React?

You may access the child state by passing a callback to the child component. Now if you click the button in the child component, you will execute the function passed from the parent and have access to the child component's state variables.

How do you access state variables in React?

The state and props in React are always in an object format. This means that the value could be accessed from the state and props via key-value pair. To access the normal state object, you can use the key name from the object.

How do you select a child element in React?

Access a DOM Element Using ReactDOM.findDOMNode() . findDOMNode() is used to find a specific node from the DOM tree and access its various properties, such as its inner HTML, child elements, type of element, etc. In short, it can return all the supported DOM measurements related to the kind of element.


1 Answers

I have done similar thing in a project by passing state of parent as a prop in child to access child component data in parent component for form elements.

In your case if you send component's state in its child as a prop and each child use state of parent like this.props.state.variablename and not this.state.variablename. You will have control on child components' states / data.

Sending state to childrens from form component using this.prop.children as a prop is not straight forward. Below link helps in doing this.

https://stackoverflow.com/a/32371612/1708333

Example:

Parent component:

<FormFields
    state={this.state}
    _onChange={this._onChange}
/>

Child component

<Input
    name="fieldname"
    value={this.props.state.fieldname}
    type="text"
    label="Lable text"
    validationMessage={this.props.state.validationMessages.fieldname} 
    onChange={this.props._onChange}
/>

Let me know if you need more information.

like image 89
Balram Singh Avatar answered Oct 01 '22 08:10

Balram Singh