javascript - how to update parent's state from child component along with passing some information from child - Stack Ov

I am trying to build a portal where I have 3 ponents. Parent ponent->Child 1->Child 2From main po

I am trying to build a portal where I have 3 ponents.

Parent ponent

->Child 1

->Child 2

From main ponent

-if (no files are selected) then GUI should show just the Child 1 and Count and pathnames of files associated with Application

-else if (user has clicked on any file) then GUI should show the filename and nodes associated to it.

I am trying to achieve this but i am confused in how to pass info from parent to child and vice versa.

In the code given below in Child 1.js when user click on path, the Parent ponent should update the GUI view by calling Child2 rather than calling Child1 .

How can i achieve this?

I am trying to build a portal where I have 3 ponents.

Parent ponent

->Child 1

->Child 2

From main ponent

-if (no files are selected) then GUI should show just the Child 1 and Count and pathnames of files associated with Application

-else if (user has clicked on any file) then GUI should show the filename and nodes associated to it.

I am trying to achieve this but i am confused in how to pass info from parent to child and vice versa.

In the code given below in Child 1.js when user click on path, the Parent ponent should update the GUI view by calling Child2 rather than calling Child1 .

How can i achieve this?

Share Improve this question edited Jan 23, 2019 at 16:07 hightides1973 asked Dec 18, 2018 at 13:46 hightides1973hightides1973 5272 gold badges11 silver badges29 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

For update parent ponent state from child ponent with arguments. You need to create method in parent ponent, that set state from arguments from this method. And pass this method to child ponent by props.

class Parent extends React.Component {
    state = {text: ""}

    updateText = text => {
        this.setState({text: text})
    }

    render () {
        return (<Child updateText={this.updateText}>)
    }
}

class Child extends React.Component {
    render () {
        return (
            <button 
                onClick={
                    () => this.props.updateText("updated state from child ponent")
                }
            >Update State</button>
        )
    }
}

Building on what galishmann provided, just pass y.Filename to the function in props as it already expects a vlaue as a parameter.

class Parent extends React.Component {
    state = { text: "" }

    updateText = text => {
        this.setState({ text: text })
    }

    render() {
        return (<Child updateText={this.updateText} />)
    }
}

class Child extends React.Component {
    ....
    ....
    ....

    render() {
        const { updateText } = this.props;
        const pathElement = this.state.groupedByAppName.map((x) => {
            return (
                <Collapsible trigger={x.AppName + '\t' + x.Count + ' files'} transitionTime={20}>
                    {
                        x.section.map((y) => <p filename={y.FileName} onClick={() => updateText(y.Filename)}>{y.Path}</p>)
                    }
                </Collapsible>
            )
        })
        return <div> {pathElement} </div>
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742350396a4427398.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信