最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send data from stateless child component to stateless parent component in react? - Stack Overflow

programmeradmin1浏览0评论

I am new to react. I have a react App with several class ponents and classless ponents. I have following two ponents and need to send a data ("projectID" in this case) from the child ponent to parent ponent. My child ponent is a classless ponent. I know that I can do this in class ponents as follows

sendId(id) {
  this.setState({ projectID:id }, () =>
     this.props.onClick(this.state.projectID)
  )
}

But I have a classless child ponent

Child ponent:

const sendId = (id) => {
  // How to setup "send id to parent" here?
}

const project = ({ task }) => (
  <div onClick={() => sendId(task.projectID)} >
    {task.projectID}
  </div>
)

export default project

Parent ponent:

import project from './project'

class ProjectList extends React.Component {

  render() {
    return (
      <div>
        {() => (
          <project 
              // How to setup "get id from child" here? 
          />
        )}
      </div>
    ));
  }
}

How can I do this?

I am new to react. I have a react App with several class ponents and classless ponents. I have following two ponents and need to send a data ("projectID" in this case) from the child ponent to parent ponent. My child ponent is a classless ponent. I know that I can do this in class ponents as follows

sendId(id) {
  this.setState({ projectID:id }, () =>
     this.props.onClick(this.state.projectID)
  )
}

But I have a classless child ponent

Child ponent:

const sendId = (id) => {
  // How to setup "send id to parent" here?
}

const project = ({ task }) => (
  <div onClick={() => sendId(task.projectID)} >
    {task.projectID}
  </div>
)

export default project

Parent ponent:

import project from './project'

class ProjectList extends React.Component {

  render() {
    return (
      <div>
        {() => (
          <project 
              // How to setup "get id from child" here? 
          />
        )}
      </div>
    ));
  }
}

How can I do this?

Share Improve this question asked Apr 1, 2019 at 8:25 David JohnsDavid Johns 1,7346 gold badges23 silver badges60 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

If I understand your question, you can access the id passed to sendId() from the ProjectList ponent by passing a callback prop to <project> in the ProjectList render method like so:

class ProjectList extends React.Component {

  render() {
    return (
      <div>
          {/* pass onSendId callback prop, and console log when it's invoked */}
          <project onSendId={ (theId) => { console.log(`sent id: ${theId}`); } } />
      </div>
    ));
  }
}

This change would then require your to update the project ponent so that the callback prop is invoked when the onClick handler is invoked:

const project = ({ task, onSendId }) => (
  {/* Pass the onSendId callback into project, and call this from onClick */}
  <div onClick={() => onSendId(task.projectID)} >
    {task.projectID}
  </div>
)

Hope that helps!

Just give parent's method to child ponent, and use props.xxx().

https://jsfiddle/bugqsfwj/

You can pass a callback that you define in your parent ponent to your child

ponent props.

child ponent:

const project = ({ task, sendId }) => (
  <div onClick={() => sendId(task.projectID)} >
    {task.projectID}
  </div>
)

export default project

parent ponent :

import project from './project'

class ProjectList extends React.Component {

  sendIdHandler = (projectId) => {
    //put your logic here
  }

  render() {
    return (
      <div>
          <project sendId={this.sendIdHandler} />
      </div>
    ));
  }
}

All you have to do is passing a function to child ponent that accept ID.

Parent Component

class ProjectList extends React.Component {
  getChildData = (id) => {/* do something */ }

  render() {
    return (
      <div><Project getDataFn={this.getChildData}</div>
    ));
  }
}

Child Component

const project = ({ task }) => (
  <div onClick={this.props.getDataFn(task.projectID)} >
    {task.projectID}
  </div>
)

Happy React :)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论