首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >切换子组件上的活动类

切换子组件上的活动类
EN

Stack Overflow用户
提问于 2016-03-20 15:16:02
回答 2查看 2.8K关注 0票数 1

我有点头疼,想找出实现这个方法的反应方式。

我有一个包含SearchItems的搜索组件,当单击一个项时,我需要将它的状态设置为active,以便它得到正确的CSS,我设法使它正常工作,但是我如何从其他对象中删除活动状态呢?

我在想,我可以从顶层组件中传递一个函数,该函数将接受搜索的ID,单击它就会在SearchItems中压缩,并根据ID的不同将它们的状态更改为true/false?

下面是密码!

顶层构成部分:

代码语言:javascript
运行
复制
    import React from "react";
import {Link} from "react-router";

import Search from "./Search";

    export default class Searches extends React.Component {
      constructor(){
        super();
        this.state = {
          searches : [
          {
            id : "2178348216",
            searchName: "searchName1",
            matches: "5"

          },
          {
            id : "10293840132",
            searchName: "searchName2",
            matches: "20"

          }

          ]
        };

      }

      render() {
        const { searches } = this.state;

        const SearchItems = searches.map((search) => {
          return <Search key={search.id} {...search}/>

        })

        return (
         <div> {SearchItems} </div>
          );

      }
    }

搜索项组件

代码语言:javascript
运行
复制
export default class Search extends React.Component {
  constructor() {
    super();

    // Set the default panel style

    this.state = {
      panelStyle: { height: '90px', marginBottom: '6px', boxShadow: '' },
      selected: false
    }



  }

  isActive(){
    return 'row panel panel-success ' + (this.state.selected ? 'active' : 'default');
  }

viewNotifications(e){
  this.setState({selected: true});

}
    render() {
      const { id, searchName, matches } = this.props;



      const buttonStyle = {
        height: '100%',
        width: '93px',
        backgroundColor: '#FFC600'
      }

    return (
     <div style={this.state.panelStyle} className={this.isActive()}>
       <div class="col-xs-10">
       <div class="col-xs-7">
         Search Name: {searchName}
       </div>
        <div class="col-xs-7">
         Must Have: PHP, MySQL
       </div>
       <div class="col-xs-7">
         Could Have: AngularJS
       </div>

       </div>

          <button type="button" onClick={this.viewNotifications.bind(this)} style={buttonStyle} class="btn btn-default btn-lg"> {matches} </button>



     </div>



    );
  }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-20 22:44:20

我想你根本不需要子组件中的状态。事实上,在大多数组件中避免状态是一个好主意,因此它们易于推理和重用。

在这种情况下,我只会将所有状态保留在父组件上。

最上面的组成部分:

代码语言:javascript
运行
复制
import React from "react";

import Search from "./search";

export default class Searches extends React.Component {
  constructor(){
    super();
    this.state = {
      searches : [
        {
          id : "2178348216",
          searchName: "searchName1",
          matches: "5"
        },
        {
          id : "10293840132",
          searchName: "searchName2",
          matches: "20"
        }
      ],
      activeElement : null
    };
  }

  _onSearchSelect(searchId) {
    this.setState({'activeElement': searchId})
  }

  render() {
    const { searches, activeSearchId } = this.state;

    const SearchItems = searches.map((search) => {
      return <Search key={search.id} {...search}
        isActive={search.id === activeElement}
        onSelect={this._onSearchSelect.bind(this)} />
    })

    return (
     <div> {SearchItems} </div>
    );
  }
}

儿童部分:

代码语言:javascript
运行
复制
import React from "react";

export default class Search extends React.Component {
  _getPanelClassNames() {
    const { isActive } = this.props
    return 'row panel panel-success ' + (isActive ? 'active' : 'default')
  }

  _onSelect() {
    const { id, onSelect } = this.props;
    onSelect(id)
  }

  render() {
    const { searchName, matches } = this.props;
    const panelStyle = { height: '90px', marginBottom: '6px', boxShadow: '' }
    const buttonStyle = {
      height: '100%',
      width: '93px',
      backgroundColor: '#FFC600'
    }
    return (
      <div style={panelStyle} className={this._getPanelClassNames()}>
        <div className="col-xs-4">
          Search Name: {searchName}
        </div>
        <div className="col-xs-3">
          Must Have: PHP, MySQL
        </div>
        <div className="col-xs-3">
         Could Have: AngularJS
        </div>
        <div className="col-xs-2">
          <button type="button" onClick={this._onSelect.bind(this)}
            style={buttonStyle} className="btn btn-default btn-lg"
          >
            {matches}
          </button>
        </div>
      </div>
    );
  }
}

您还可以看到它在柱塞中运行:https://plnkr.co/edit/sdWzFedsdFx4MpbOuPJD?p=preview

票数 2
EN

Stack Overflow用户

发布于 2016-03-20 16:15:26

好的,事实证明,这比我想象的更简单,它只是一个理解react如何工作的案例(而不是被混淆)。

当您有一个顶级组件时,您可以通过道具将它的状态传递给子组件,当您更新顶层组件中的状态时,它将向下传递给子组件,您可以使用componentWillReceiveProps来执行操作。

我向顶层组件添加了一个名为updateActiveSearch的函数,它简单地设置了顶层组件的状态,然后将activeElement状态与函数一起传递给子元素作为支柱。当一个子元素调用此函数将自己设置为active时,所有这些元素都会触发componentWillReceiveProps,他们只需要检查自己收到的ID,如果它与活动的ID匹配,如果不匹配,则不需要!

所以我的顶层组件现在如下所示:

代码语言:javascript
运行
复制
export default class Searches extends React.Component {
  constructor(){
    super();
    this.state = {
      searches : [
      {
        id : "2178348216",
        searchName: "searchName1",
        matches: "5"

      },
      {
        id : "10293840132",
        searchName: "searchName2",
        matches: "20"

      }

      ],
      activeElement : 0
    };


  }
// This function gets passed via a prop below
  updateActiveSearch(id){
    //console.log(id);
    this.setState({activeElement : id});
  }

  render() {


    const SearchItems = this.state.searches.map((search) => {
      return <Search activeElement={this.state.activeElement} goFunction={this.updateActiveSearch.bind(this)} key={search.id} {...search}/>

    })

    return (
     <div> {SearchItems} </div>
      );

  }
}

子组件

代码语言:javascript
运行
复制
export default class Search extends React.Component {
  constructor() {
    super();

    // Set the default panel style

    this.state = {
      panelStyle: { height: '90px', marginBottom: '6px', boxShadow: '' },
      selected: false
    }

  }


// This happens right before the props get updated!   
    componentWillReceiveProps(incomingProps){
      if(incomingProps.activeElement == this.props.id){
        this.setState({selected: true});
      } else {
        this.setState({selected: false});
      }
}

  isActive(){
    return 'row panel panel-success ' + (this.state.selected ? 'active' : 'default');
  }

viewNotifications(e){
  //this.state.panelStyle.boxShadow = '-2px 3px 20px 5px rgba(255,198,0,1)';
  this.setState({selected: true});
  this.props.goFunction(this.props.id);

}
    render() {
      const { id, searchName, matches } = this.props;

      const buttonStyle = {
        height: '100%',
        width: '93px',
        backgroundColor: '#FFC600'
      }

    return (
     <div style={this.state.panelStyle} className={this.isActive()}>
       <div class="col-xs-10">
       <div class="col-xs-7">
         Search Name: {searchName}
       </div>
        <div class="col-xs-7">
         Must Have: PHP, MySQL
       </div>
       <div class="col-xs-7">
         Could Have: AngularJS
       </div>

       </div>

          <button type="button" onClick={this.viewNotifications.bind(this)} style={buttonStyle} class="btn btn-default btn-lg"> {matches} </button>



     </div>



    );
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36115540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档