Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何动态更改已设置样式的组件的样式?_Javascript_Reactjs_Styled Components - Fatal编程技术网

Javascript 如何动态更改已设置样式的组件的样式?

Javascript 如何动态更改已设置样式的组件的样式?,javascript,reactjs,styled-components,Javascript,Reactjs,Styled Components,我目前正在学习如何在React中使用样式化组件,并且在实现这一点时遇到了困难 我有一行定义为div的按钮。当一个按钮被点击时,我希望它的背景用某种颜色填充。所有其他按钮应保持“未选中”。以下是我到目前为止的情况: 从“React”导入React; 从“样式化组件”导入样式; const ButtonsRow=styles.div` 显示器:flex; 对正内容:空间均匀; `; const按钮=styles.div` 光标:指针; :悬停{ 背景颜色:灰色; } 背景色:${props=>pr

我目前正在学习如何在React中使用样式化组件,并且在实现这一点时遇到了困难

我有一行定义为div的按钮。当一个按钮被点击时,我希望它的背景用某种颜色填充。所有其他按钮应保持“未选中”。以下是我到目前为止的情况:

从“React”导入React; 从“样式化组件”导入样式; const ButtonsRow=styles.div` 显示器:flex; 对正内容:空间均匀; `; const按钮=styles.div` 光标:指针; :悬停{ 背景颜色:灰色; } 背景色:${props=>props.selected?'red':'none'}; `; 类按钮容器扩展了React.Component{ handleClick=e=>{ //是否将“选定”道具添加到单击的? } 渲染{ 回来 人 成员 游戏 ; } } 导出默认按钮容器 您需要管理每个按钮的状态

所有解决方案在如何将按钮状态管理为单个对象/数组等方面都会有所不同。主要概念是获取按钮的id,以便了解所指的状态

在下一个简单示例中,我使用curried函数提供按钮id

另一个简单的解决方案是将id属性传递给您的按钮,并在单击按钮时查询它

const ButtonsRow = styled.div`
  display: flex;
  justify-content: space-evenly;
`;

const Button = styled.div`
  cursor: pointer;
  :hover {
    background-color: gray;
  }

  background-color: ${props => (props.selected ? 'red' : 'none')};
`;

class ButtonsContainer extends React.Component {
  state = {
    first: false,
    second: false,
    third: true
  };

  toggle = buttonName => () => {
    this.setState(prev => ({ [buttonName]: !prev[buttonName] }));
  };

  render() {
    const { first, second, third } = this.state;
    return (
      <ButtonsRow>
        <Button selected={first} onClick={this.toggle('first')}>
          People
        </Button>
        <Button selected={second} onClick={this.toggle('second')}>
          Members
        </Button>
        <Button selected={third} onClick={this.toggle('third')}>
          Games
        </Button>
      </ButtonsRow>
    );
  }
}
您需要管理每个按钮的状态

所有解决方案在如何将按钮状态管理为单个对象/数组等方面都会有所不同。主要概念是获取按钮的id,以便了解所指的状态

在下一个简单示例中,我使用curried函数提供按钮id

另一个简单的解决方案是将id属性传递给您的按钮,并在单击按钮时查询它

const ButtonsRow = styled.div`
  display: flex;
  justify-content: space-evenly;
`;

const Button = styled.div`
  cursor: pointer;
  :hover {
    background-color: gray;
  }

  background-color: ${props => (props.selected ? 'red' : 'none')};
`;

class ButtonsContainer extends React.Component {
  state = {
    first: false,
    second: false,
    third: true
  };

  toggle = buttonName => () => {
    this.setState(prev => ({ [buttonName]: !prev[buttonName] }));
  };

  render() {
    const { first, second, third } = this.state;
    return (
      <ButtonsRow>
        <Button selected={first} onClick={this.toggle('first')}>
          People
        </Button>
        <Button selected={second} onClick={this.toggle('second')}>
          Members
        </Button>
        <Button selected={third} onClick={this.toggle('third')}>
          Games
        </Button>
      </ButtonsRow>
    );
  }
}

您必须创建一个变量来存储每个按钮的状态。一种更简单的方法可能是从数组中动态生成按钮,并使用它来维护状态

class ButtonsContainer extends React.Component {
    state = {
       buttons = [{label:"People"},{label:"Members"},{label:"Games"}]
    }

    handleClick = (button) => (e) => {
      this.setState((prevState) => ({
           buttons: prevState.buttons.filter(btn => btn.label !== button.label)
                                .concat({...button,selected:!button.selected})
      })
    }

    render() {
        return(
            <ButtonsRow>
                {this.state.buttons.map(button => (<Button key={button.label} selected={button.selected} onClick={this.handleClick(button)}>{button.label}</Button>))}
            </ButtonsRow>  
        );
    }
}

export default ButtonsContainer;

您必须创建一个变量来存储每个按钮的状态。一种更简单的方法可能是从数组中动态生成按钮,并使用它来维护状态

class ButtonsContainer extends React.Component {
    state = {
       buttons = [{label:"People"},{label:"Members"},{label:"Games"}]
    }

    handleClick = (button) => (e) => {
      this.setState((prevState) => ({
           buttons: prevState.buttons.filter(btn => btn.label !== button.label)
                                .concat({...button,selected:!button.selected})
      })
    }

    render() {
        return(
            <ButtonsRow>
                {this.state.buttons.map(button => (<Button key={button.label} selected={button.selected} onClick={this.handleClick(button)}>{button.label}</Button>))}
            </ButtonsRow>  
        );
    }
}

export default ButtonsContainer;