简体   繁体   English

从无状态组件执行状态方法

[英]Executing a state method from the stateless component

I have a page component let's say HomePage . 我有一个页面组件,比如说HomePage And I have two stateless components called List and ListRow . 我有两个名为ListListRow无状态组件。 The idea is to show the Table with the list of items and when the user clicks on any particular row, hide the table and show the details of that particular row. 想法是显示带有项目列表的表格,当用户单击任何特定行时,隐藏表格并显示该特定行的详细信息。

So in my HomePage render method: 所以在我的HomePage渲染方法中:

<List data={listOfData} onClick={this.clickHanlder} />

The HomePage click handler method: HomePage点击处理程序方法:

clickHandler(name) {
  console.log(name);
}

And in my List stateless component I do this: List无状态组件中,我这样做:

const List = ({data, onClick}) => {
  return (
    <table>
      <thead>
        <tr>
          <td>Name</td>
        </tr>
      </thead>
      <tbody>
        {data.map(el => 
          <ListRow key={el.id} item={el} onClick={onClick} />
        )}
      </tbody>
    </table>
  );
}

And in my ListRow stateless component I do this: 在我的ListRow无状态组件中,我这样做:

const ListRow = ({item, onClick}) => {
  return (
    <tr>
      <td><a href="#" onClick={onClick(el.name)}>{el.name}</a></td>
    </tr>
  );
}

The click handler method in the HomePage executes once the page loads without even clicking on the row. HomePage加载后,单击处理程序方法将在页面加载后立即执行,甚至无需单击该行。 When I click on the row, nothing happens. 当我单击该行时,没有任何反应。

I'm stuck at how and where to handle the click event, so that I hide the table and show the details of the row. 我被困在如何以及在何处处理click事件的问题,以便隐藏表格并显示行的详细信息。 I also should implement close / back button to show the table and hide the details. 我还应该实现关闭/返回按钮来显示表格并隐藏细节。

What I have done so far is implementing the click event method in HomePage which will update its state and pass that method down till the ListRow stateless component. 到目前为止,我所做的是在HomePage实现click事件方法,该方法将更新其状态并将该方法向下传递到ListRow无状态组件。 But when I load the app itself, the click event gets executed. 但是,当我加载应用程序本身时,click事件就会执行。 I'm not sure how to handle this properly. 我不确定如何正确处理。

It's idiomatic in React to implement container components that will handle (for example) click events, population etc. So in your case you can create a handler for the row/item click in the List component and pass a reference to that down to the ListRow component as one of its props . 在React中,惯用的是实现将处理(例如)单击事件,填充等事件的容器组件。因此,在您的情况下,您可以在List组件中为行/项单击创建处理程序,并将对该引用的引用传递给ListRow组件作为其道具之一

Even though it's stateless, your List component can still declare a function to handle the click; 即使它是无状态的,您的List组件仍可以声明一个函数来处理单击。 look at the example shown at the top of this article: 查看本文顶部显示的示例:

https://medium.com/@housecor/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc#.9sxosb40u https://medium.com/@housecor/react-stateless-functional-components-nine-wins-you-might-have-overlooked-997b0d933dbc#.9sxosb40u

However you can't use any of the React lifecycle methods or state. 但是,您不能使用任何React生命周期方法或状态。 If you want to use these you would need to wrap it as shown here: 如果要使用这些,则需要如下所示将其包装起来:

https://egghead.io/lessons/javascript-redux-fetching-data-on-route-change https://egghead.io/lessons/javascript-redux-fetching-data-on-route-change

I found the solution finally. 我终于找到了解决方案。 I was doing it wrong. 我做错了。 I've changed the clickHandler to as follows: 我将clickHandler更改为如下:

clickHandler(event) {
  console.log(event.target.innerHTML);
}

and in my ListRow stateless component, I'm doing this: 在我的ListRow无状态组件中,我正在这样做:

const ListRow = ({item, onClick}) => {
  return (
    <tr>
      <td><a href="#" onClick={onClick}>{el.name}</a></td>
    </tr>
  );
}

The mistake I was doing is that I was trying to pass the value to the onClick event which seems doesn't work. 我正在做的错误是我试图将值传递给onClick事件,这似乎不起作用。 So using the innerHTML resolved the problem. 因此,使用innerHTML解决了该问题。

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

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