在 Material-UI 中使用 React 的“ref”属性

Joh*_*amy 9 javascript reactjs material-ui

我正在尝试在 Material-UI 的 TextField 上使用 React 的“ref”属性访问输入数据。通过“inputRef”或“inputProps”似乎没有一种简单的方法可以做到这一点。

下面的示例显示了第 26 行 inputProps 的使用。将 TextField 的名称分配给 'ref' 属性似乎不会产生有效的对象。

使用“inputRef”,根据Material-ui文档强制使用函数,尝试将字段数据作为属性传递也不起作用。例如:(txt => this.name = txt)

有没有人找到解决办法?

class MediaForm extends Component {
  constructor (props) {
    super(props)
    this.state = {}
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  handleSubmit (e) {
    const { title, colour } = this.refs
    e.preventDefault()
    window.alert(`New colour: ${title.value} ${colour.value}`)
  }

  render () {
    const { classes } = this.props
    return (
      <form className={classes.root}
        onSubmit={this.handleSubmit}>
        <div className={classes.field}>
          <TextField
            name='title'
            type='text'
            label='Title'
            placeholder='Movie title...'
            autoFocus
            inputProps={{ref: this.name}}
            required />
        </div>
        <div className={classes.field}>
          <Tooltip title='Add a colour the reflects the mood of the film'>
            <TextField
              name='colour'
              type='color'
              label='Mood'
              inputProps={{ref: this.name}}
              required />
          </Tooltip>
        </div>
        <Button
          variant='raised'
          color='primary'
          className={classes.button}>
          ADD
        </Button>
      </form>
    )
  }
}

MediaForm.propTypes = {
  classes: PropTypes.object.isRequired
}

export default withRoot(withStyles(styles)(MediaForm))
Run Code Online (Sandbox Code Playgroud)

tri*_*ixn 6

你不需要参考。submit 事件包含表单作为目标。您可以通过以下方式访问表单中的输入form.elements

handleSubmit (event) {
    const { title, colour } = event.currentTarget.elements;
    event.preventDefault();
    window.alert(`New colour: ${title.value} ${colour.value}`);
}
Run Code Online (Sandbox Code Playgroud)

关于你的裁判的问题:this.name指的是什么?你没有在任何地方声明它,所以它是undefined。传递undefinedref道具没有效果。此外,如何将两个输入引用绑定到同一个实例属性name。您是否知道this渲染函数中的 指的是MediaForm组件的实例,因此this.namename组件实例上的一个属性(未定义)?

如果您想为每个输入获取单独的 refs,您应该使用回调模式。请注意,不推荐使用字符串引用,不应使用:

render() {
    return(
        <TextField
            name='title'
            type='text'
            label='Title'
            placeholder='Movie title...'
            autoFocus
            inputProps={{ref: input => this.titleInput = input}}
            required 
        />
    );
}
Run Code Online (Sandbox Code Playgroud)

编辑:

您可能想要的是所谓的受控组件。在这种模式中,您的父组件会跟踪其子组​​件的值(通常是输入):

class MediaForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            title: '',
            colour: '',
        };
    }

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

    handleSubmit = event => {
        event.preventDefault();
        const {title, colour} = this.state;
        window.alert(`New colour: ${title} ${colour}`);
    };

    render() {
        const {classes} = this.props;
        const {title, colour} = this.state;

        return (
            <form className={classes.root} onSubmit={this.handleSubmit}>
                <div className={classes.field}>
                    <TextField
                        name="title"
                        type="text"
                        value={title}
                        onChange={this.handleChange}
                        label="Title"
                        placeholder="Movie title..."
                        required
                    />
                </div>
                <div className={classes.field}>
                    <Tooltip title="Add a colour the reflects the mood of the film">
                        <TextField
                            name="colour"
                            type="color"
                            value={colour}
                            onChange={this.handleChange}
                            label="Mood"
                            required
                        />
                    </Tooltip>
                </div>
                <Button
                    type="submit"
                    variant="raised"
                    color="primary"
                    className={classes.button}
                >
                    ADD
                </Button>
            </form>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑 kxm42q76vv

现在,您的父母可以通过this.state.title和完全控制和访问每个输入的值this.state.colour。这里也不需要任何参考。