TypeError: Object(...) is not a function React
TypeError: Object(...) is not a function React
我正在从这个 channel. Recently, I stumbled upon React Hooks from here 学习 React。因此,我尝试将基于 class 的组件转换为基于钩子的组件。这是我的基于 class 的组件:
import React, { Component } from 'react';
class AddNinja extends Component {
state = {
name: null,
age: null,
skill: null,
}
handleChange = e => {
this.setState({
[e.target.id]: e.target.value,
})
}
handleSubmit = e => {
e.preventDefault();
this.props.addNinja(this.state);
}
render() {
return (
<div>
<form onSubmit={ this.handleSubmit }>
<label htmlFor="name">Name: </label>
<input type="text" id="name" onChange={ this.handleChange } />
<label htmlFor="age">Age: </label>
<input type="number" id="age" onChange={ this.handleChange } />
<label htmlFor="skill">Skill: </label>
<input type="text" id="skill" onChange={ this.handleChange } />
<button>Submit</button>
</form>
</div>
)
}
}
这是我转换后的组件:
https://codesandbox.io/s/n0lw4wo550?module=%2Fsrc%2FAddNinja.js
但我收到以下错误:
React 挂钩在 React v16.8.0
中可用。将你的 React 和 React dom 版本更新为 16.8.0
。
"react": "16.8.0",
"react-dom": "16.8.0",
这是更新版本的代码:https://codesandbox.io/s/qq90900xr4
我正在从这个 channel. Recently, I stumbled upon React Hooks from here 学习 React。因此,我尝试将基于 class 的组件转换为基于钩子的组件。这是我的基于 class 的组件:
import React, { Component } from 'react';
class AddNinja extends Component {
state = {
name: null,
age: null,
skill: null,
}
handleChange = e => {
this.setState({
[e.target.id]: e.target.value,
})
}
handleSubmit = e => {
e.preventDefault();
this.props.addNinja(this.state);
}
render() {
return (
<div>
<form onSubmit={ this.handleSubmit }>
<label htmlFor="name">Name: </label>
<input type="text" id="name" onChange={ this.handleChange } />
<label htmlFor="age">Age: </label>
<input type="number" id="age" onChange={ this.handleChange } />
<label htmlFor="skill">Skill: </label>
<input type="text" id="skill" onChange={ this.handleChange } />
<button>Submit</button>
</form>
</div>
)
}
}
这是我转换后的组件: https://codesandbox.io/s/n0lw4wo550?module=%2Fsrc%2FAddNinja.js
但我收到以下错误:
React 挂钩在 React v16.8.0
中可用。将你的 React 和 React dom 版本更新为 16.8.0
。
"react": "16.8.0",
"react-dom": "16.8.0",
这是更新版本的代码:https://codesandbox.io/s/qq90900xr4