如何确定反应中的material-ui滑块? [英] How do I identify a material-ui slider in react?

查看:34
本文介绍了如何确定反应中的material-ui滑块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望一个React组件中的多个material-ui滑块共享一个公共事件处理程序.但是,要使其工作,我需要确定原始滑块.从 API文档中,我看不到它是如何实现的.我尝试将 id name 属性应用于< Slider> -component,但在合成事件中没有看到这些在事件处理程序中.

I want multiple material-ui sliders in one react component sharing a common event handler. However, to make this work, I would need to identify the originating slider. From the API documentation I can't see how that is achieved. I've tried applying id and name attributes to the <Slider>-component, yet I'm not seeing these in the synthesized event in the event handler.

handleChange = (event, value) => {
  console.log(event); // 'Id' and 'name' attributes in 'target' are empty
  this.setState({ value });
};

render() {
  const { classes } = this.props;
  const { value } = this.state;

  return (
    <div className={classes.root}>
      <Typography id="label">Slider label</Typography>
      <Slider
        classes={{ container: classes.slider }}
        value={value}
        aria-labelledby="label"
        onChange={this.handleChange}
      />
    </div>
  );
}

这是从官方演示项目中获取的:

This is fetched from the official demo project:

https://codesandbox.io/s/4j9l9xn1o4

任何帮助将不胜感激!

推荐答案

您可以像这样设置状态:

You can format your state like so:

state = {
  slider1: 50, //slider1 is the name of the first slider
  slider2: 50, //slider2 is the name of the second slider
}

在那之后,您有2种方法可以设置更改滑块值时的状态:

After that, you have 2 ways to set the state when the value of the slider is changed:

  • (更新:),该方法不起作用!不过,我将其留在此处以备将来参考)通过使用HTML属性 id ,然后通过使用 event.target.id .整个 handleChange 方法如下所示:

  • (Update: This method doesn't work! However I will leave it here for future reference) By using HTML attribute id, then access it by using event.target.id. The whole handleChange method would look like this:

handleChange = (e, value) => {
  this.setState({
    [e.target.id]: value
  });
}

  • 然后将滑块的名称直接传递给 handleChange 方法,就像这样:

    handleChange = name => (e, value) => {
      this.setState({
        [name]: value
      });
    }
    

  • 总体而言,您的组件应为:

    Overall, your component should be:

    class SimpleSlider extends Component {
      state = {
        slider1: 50,
        slider2: 50
      };
    
      handleChange = name => (e, value) => {
        this.setState({
          [name]: value // --> Important bit here: This is how you set the value of sliders
        });
      };
    
      render() {
        const { classes } = this.props;
        const { slider1, slider2 } = this.state;
    
        return (
          <div className={classes.root}>
            <Typography id="label">Slider label</Typography>
            <Slider
              classes={{ container: classes.slider }}
              value={slider1}
              aria-labelledby="label"
              onChange={this.handleChange("slider1")}
            />
            <Slider
              classes={{ container: classes.slider }}
              value={slider2}
              aria-labelledby="label"
              onChange={this.handleChange("slider2")}
            />
          </div>
        );
      }
    }
    

    查看实际运行情况: https://codesandbox.io/s/4qz8o01qp4

    在运行代码之后,我发现#1无效,因为id属性没有向下传递给事件目标

    After running the code I found that the #1 doesn't work because the id attribute is not being passed down to the event target

    这篇关于如何确定反应中的material-ui滑块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆