我试图在响应中从文本字段获取输入,但是它不起作用,我也不知道为什么。我看过很多不同的解决方案,但似乎都没有用。

我什至尝试遵循此https://reactjs.org/docs/refs-and-the-dom.html,但是我无法正确理解?

class Activity extends Component {

constructor(props) {
    super(props);
    this.newActivity = React.createRef();
}

callAPI() {
    fetch("http://localhost:5000/activities", {
        method: 'POST',
        body: JSON.stringify({
            newName: this.newActivity.current,
            oldName: this.state.activity
        }),
    })
        .then(function (response) {
            return response.json()
        });
}



state = {
    activity: this.props.data.name
}
render() {
    return (
        <React.Fragment>
            <Grid justify="center" container spacing={(2, 10)} aligncontent="center">
                <Grid item xs={8} >
                    <Paper>
                        {/*Trying to get the input here so that I can put it into my POST request*/}
                        <TextField inputRef={el => this.newActivity = el} type="activity" id="standard-basic" label="Standard" defaultValue={this.state.activity} />
                    </Paper>
                </Grid>
                <Grid item xs={2}>
                    <Button onClick={this.callAPI} variant="contained" startIcon={<UpdateIcon />} style={buttonStyle} >Uppdatera</Button>
                </Grid>
                <Grid item xs={2}>
                    <Button variant="contained" startIcon={<DeleteIcon />} style={buttonStyle} >Ta Bort</Button>
                </Grid>

            </Grid>
        </React.Fragment>
        );
    }
}


我得到的错误是


  TypeError:无法读取未定义的属性“ newActivity”

最佳答案

您必须在构造函数中初始化状态值。

还要将此行更改为inputRef={this.newActivity}而不是inputRef={(el)=>this.newActivity =el}。因为您已经使用createRef创建了引用,所以无需再次创建。

class Activity extends Component {

constructor(props) {
    super(props);
    this.state = {
        activity: this.props.data.name
    }
    this.callAPI = this.callAPI.bind(this);
    this.newActivity = React.createRef();
}

callAPI() {
    fetch("http://localhost:5000/activities", {
        method: 'POST',
        body: JSON.stringify({
            newName: this.newActivity.current,
            oldName: this.state.activity
        }),
    })
        .then(function (response) {
            return response.json()
        });
}

render() {
    return (
        <React.Fragment>
            <Grid justify="center" container spacing={(2, 10)} aligncontent="center">
                <Grid item xs={8} >
                    <Paper>
                        {/*Trying to get the input here so that I can put it into my POST request*/}
                        <TextField inputRef={this.newActivity} type="activity" id="standard-basic" label="Standard" defaultValue={this.state.activity} />
                    </Paper>
                </Grid>
                <Grid item xs={2}>
                    <Button onClick={this.callAPI} variant="contained" startIcon={<UpdateIcon />} style={buttonStyle} >Uppdatera</Button>
                </Grid>
                <Grid item xs={2}>
                    <Button variant="contained" startIcon={<DeleteIcon />} style={buttonStyle} >Ta Bort</Button>
                </Grid>

            </Grid>
        </React.Fragment>
    );
}

09-18 04:58