【发布时间】:2018-10-09 23:10:51
【问题描述】:
在 React 组件之外的函数中使用 setState 有错吗?
例子:
// myFunction.js
function myFunction() {
...
this.setState({ ... })
}
// App.js
import myFunction from './myFunction
class App extends Component {
constructor() {
super()
this.myFunction = myFunction.bind(this)
}
...
}
【问题讨论】:
-
没错。您实际上是在创建一个 mixin 函数。
-
@corasan 以这种方式编写的代码真的会为你运行吗?
-
@Colin 是的,确实如此,完美运行
标签: javascript reactjs react-native ecmascript-6