无法通过该函数声明setState。而且我不明白这是什么错误。
import React, { Component } from 'react';
export default class App extends Component {
state = {
text: ' ',
};
render() {
const text = () => {
this.setState({ text: 'Good afternoon' });
};
return(
)
}
}
答案 0 :(得分:0)
Functions和setState应该在渲染生命周期之外。 更多信息:https://reactjs.org/docs/state-and-lifecycle.html
SetState指南:https://blog.logrocket.com/an-imperative-guide-to-setstate-in-react-b837ceaf8304/
import React, { Component } from 'react';
export default class App extends Component {
state = {
text: " "
}
const text = () => {
this.setState({text: "Good afternoon"})
}
render() {
return(
)
}
}
答案 1 :(得分:0)
请将功能文本置于渲染功能之外。
export default class App extends Component {
state = {
text: ' ',
};
//function in here
text = () => {
this.setState({ text: 'Good afternoon' });
}
render() {
return ()
}
}
答案 2 :(得分:0)
无论如何,当我传递函数的文本超出呈现限制时,setState不起作用。