I am studying ReactJS and Redux, and I cannot figure out how to access my TextField instance when binding it to a Change call on this .
(I use material-ui for this example, but without it I would run into the same problem)
import React, {Component} from 'react'; import { connect } from 'react-redux'; import { setDataValue } from './actions/data'; import TextField from 'material-ui/lib/text-field'; import Paper from 'material-ui/lib/paper'; export class DataInput extends Component { constructor(props){ super(props); } handleTextChange() { this.props.setDataValue(document.getElementById('myField').value); } render(){ return ( <Paper style={{ width: '300px', margin: '10px', padding: '10px' }}> <TextField id='myField' defaultValue={this.props.data.value} onChange={this.handleTextChange.bind(this)} /> </Paper> ); } } export default connect( (state) => ({data: state.data}), {setDataValue} )(DataInput);
I am using a solution that I find ugly (not reusable => not very "component friendly") by setting the id on my TextField and getting its value with document.getElementById('myField').value
How could I get rid of this id and access it with something like textFieldRef.value inside my callback?
I tried without .bind(this) , which gives me access to my TextField instance through this , but I can no longer access my this.props.setDataValue() function since this not bound to my class context.
Thanks!
source share