admin管理员组文章数量:1026989
I'm learning ReactJS and Redux, and I cant figure out how to get access to my TextField instance while binding it's onChange callback to this
.
(I'm using material-ui for this example, but I would encounter quite the same problem without it)
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'm using a solution that I find ugly (not reusable => not very 'component-friendly') by setting an id
on my TextField, and accessing its value with document.getElementById('myField').value
How could I get rid of this id
, and accessing 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
isn't binded to my class context.
Thank you !
I'm learning ReactJS and Redux, and I cant figure out how to get access to my TextField instance while binding it's onChange callback to this
.
(I'm using material-ui for this example, but I would encounter quite the same problem without it)
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'm using a solution that I find ugly (not reusable => not very 'component-friendly') by setting an id
on my TextField, and accessing its value with document.getElementById('myField').value
How could I get rid of this id
, and accessing 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
isn't binded to my class context.
Thank you !
Share Improve this question edited Jan 27, 2016 at 0:04 Romain Durand asked Jan 26, 2016 at 23:58 Romain DurandRomain Durand 8361 gold badge9 silver badges22 bronze badges2 Answers
Reset to default 22Avoid using calls like document.getElementById
within a React component. React is already designed to optimise expensive DOM calls, so you're working against the framework by doing this. If you want to know how reference DOM nodes in React, read the docs here.
However, in this situation you don't actually need references. Placing a ref (or an ID) on TextField
isn't necessarily going to work. What you're referencing is a React element, not a DOM node.
Instead, make better use of the onChange
callback. The function passed to onChange
is called with an event
argument. You can use that argument to get the input value. (see React's event system.)
Your event handler should look like this:
handleTextChange(event) {
this.props.setDataValue(event.target.value);
}
And your TextField
should look like this:
<TextField
value={this.props.data.value}
onChange={event => this.handleTextChange(event)}
/>
Note: Use value NOT defaultValue.
Using state along with Redux is a bad style. So this is a design issue of the code.
Redux was meant to be used along with functional components and not class style ones.
I suggest using functional component and pass syntetic event as a parameter to onChange function like this:
const mapDispatchToProps = (dispatch) => {
return {
onChange: (value) => {
dispatch(changeAction(value))
}
}
and then use something like this inside the element:
<input onChange={ (e) => onChange(e.target.value)}
You can also pass elements own properties:
const mapDispatchToProps = (dispatch) => {
I'm learning ReactJS and Redux, and I cant figure out how to get access to my TextField instance while binding it's onChange callback to this
.
(I'm using material-ui for this example, but I would encounter quite the same problem without it)
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'm using a solution that I find ugly (not reusable => not very 'component-friendly') by setting an id
on my TextField, and accessing its value with document.getElementById('myField').value
How could I get rid of this id
, and accessing 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
isn't binded to my class context.
Thank you !
I'm learning ReactJS and Redux, and I cant figure out how to get access to my TextField instance while binding it's onChange callback to this
.
(I'm using material-ui for this example, but I would encounter quite the same problem without it)
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'm using a solution that I find ugly (not reusable => not very 'component-friendly') by setting an id
on my TextField, and accessing its value with document.getElementById('myField').value
How could I get rid of this id
, and accessing 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
isn't binded to my class context.
Thank you !
Share Improve this question edited Jan 27, 2016 at 0:04 Romain Durand asked Jan 26, 2016 at 23:58 Romain DurandRomain Durand 8361 gold badge9 silver badges22 bronze badges2 Answers
Reset to default 22Avoid using calls like document.getElementById
within a React component. React is already designed to optimise expensive DOM calls, so you're working against the framework by doing this. If you want to know how reference DOM nodes in React, read the docs here.
However, in this situation you don't actually need references. Placing a ref (or an ID) on TextField
isn't necessarily going to work. What you're referencing is a React element, not a DOM node.
Instead, make better use of the onChange
callback. The function passed to onChange
is called with an event
argument. You can use that argument to get the input value. (see React's event system.)
Your event handler should look like this:
handleTextChange(event) {
this.props.setDataValue(event.target.value);
}
And your TextField
should look like this:
<TextField
value={this.props.data.value}
onChange={event => this.handleTextChange(event)}
/>
Note: Use value NOT defaultValue.
Using state along with Redux is a bad style. So this is a design issue of the code.
Redux was meant to be used along with functional components and not class style ones.
I suggest using functional component and pass syntetic event as a parameter to onChange function like this:
const mapDispatchToProps = (dispatch) => {
return {
onChange: (value) => {
dispatch(changeAction(value))
}
}
and then use something like this inside the element:
<input onChange={ (e) => onChange(e.target.value)}
You can also pass elements own properties:
const mapDispatchToProps = (dispatch) => {
本文标签:
版权声明:本文标题:javascript - How to properly bind onChange callback to 'this' with ReactJS, Redux, and ES2015? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1738486887a1575692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论