简体   繁体   English

无状态组件上的onClick事件,用于控制该组件上的道具

[英]onClick event on stateless components controlling a prop on that Component

I have a stateless component called EmailItem : I want when I click it, I can give it a new prop via some function. 我有一个名为EmailItem的无状态组件:单击该按钮时,我可以通过一些功能为其提供新的道具。

<EmailItem key={i} onClick={// onClick function} emailData={email} read={false} />

I want when the EmailItem is click it changes the read prop to true. 我想要单击EmailItem时将EmailItem更改为true。

I understand this can be done by making EmailItem a statefull component; 我知道可以通过使EmailItem成为全状态组件来完成; however, it is an iterable component and from my understanding adding state where you don't NEED it is bad. 但是,它是一个可迭代的组件,并且据我了解,添加不需要的状态是很糟糕的。 Correct me if wrong. 如果有错请指正。

I am confused about the content of the function I would use since e.target and refs will not work. 我对要使用的函数的内容感到困惑,因为e.target和refs无法正常工作。

This read prop will change the class of an item in the stateless component. read道具将更改无状态组件中项目的类。

const EmailItem = (props) => {
  let readClass = props.emailData.read ? '--read' : ''
  return (
    <div onClick={props.onClick} className='email'>
      <div className={'email__read' + readClass} />
      <div className='email__leftside'>
        <div className='email__from'>{props.emailData.from}</div>
        <div className='email__subject'>{props.emailData.subject}</div>
        <div className={'email__body'}>{props.emailData.body}</div>
      </div>
      <div className='email__rightside'>
        <div className='email__date'>{props.emailData.date}</div>
        <div className='email__time'>{props.emailData.time}</div>
      </div>
    </div>
  )
}

The email__read className is an indicator of whether the email has been read or not email__read className指示是否已阅读电子邮件

From what I understood, You can pass function(onClick) from parent to child component and bind emaildata to that function. 据我了解,您可以将函数(onClick)从父组件传递到子组件,并将emaildata绑定到该函数。 ES6 arrow function syntax takes care of parameter binding and you can get emailData(which was clicked) in parent. ES6箭头函数语法负责参数绑定,您可以在父级中获取emailData(已单击)。

Try out following example(sorry for no css) 试用以下示例(对不起,没有CSS)

class Inbox extends React.Component {

  constructor(props) {
    super(props)

    this.state = {
      emails: [
        { read: false, from: "aaa", to: "aaato", subject: "aaasubject", body: "aaabody", date: "aaadate", time: "aaatime" },
        { read: true, from: "bbb", to: "bbbto", subject: "bbbsubject", body: "bbbbody", date: "bbbdate", time: "bbbtime" },
        { read: false, from: "aaa", to: "cccto", subject: "cccsubject", body: "cccbody", date: "cccdate", time: "ccctime" },
        { read: false, from: "ddd", to: "dddto", subject: "dddsubject", body: "dddbody", date: "ddddate", time: "dddtime" },
      ]

    }
  }

  handleClick(index, ele) {
    // ele is emaildata, do anything you want
    var newEmails = this.state.emails

    newEmails[index].read = true
    this.setState({
      emails: newEmails
    })
  }

  render() {
    return (
      <div>
        <p>Emails</p>
        {
          this.state.emails.map((e, i) => {
            return <EmailItem emailData={e} key={i} onClick={() => { this.handleClick(i, e) }} />
          })
        }
      </div>
    )
  }
}


const EmailItem = (props) => {
  let readClass = props.emailData.read ? '--read' : '--unread'
  return (
    <div onClick={props.onClick} className='email'>
      <div className={'email__read' + readClass} />
      <div className='email__leftside'>
        <p>{readClass}</p>
        <div className='email__from'>From {props.emailData.from}</div>
        <div className='email__subject'>To {props.emailData.subject}</div>
        <div className={'email__body'}>Body {props.emailData.body}</div>
      </div>
      <div className='email__rightside'>
        <div className='email__date'>Date {props.emailData.date}</div>
        <div className='email__time'>Time {props.emailData.time}</div>
      </div>

      <p>---------------------</p>
    </div>
  )
}

What you need to do to change your props is to change the state. 更改道具所需要做的就是更改状态。 It doesn't matter that the state doesn't live in this component. 状态不在此组件中并不重要。 You can pass callback from your parent component and than call this callback inside your stateless component and change state in your parent component. 您可以从父组件传递回调,然后在无状态组件内部调用此回调并更改父组件的状态。 Note: State doesn't to be in the parent. 注意:State不在父级中。 It can be higher up. 它可以更高。 This should help you: https://facebook.github.io/react/docs/lifting-state-up.html 这应该对您有帮助: https : //facebook.github.io/react/docs/lifting-state-up.html

I hope this is helpful for you. 希望对您有帮助。

May be this link can help you i jut gave its answer there 也许这个链接可以帮助您,我在那儿给出了答案

How to pass function as props in React? 如何在React中传递作为道具的功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM