- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在玩 React 并尝试将用户输入的文本保存到 state
的输入中。我在文本区域中添加了一个 onChange
属性来设置状态。
但是,当我开始输入时,我在控制台中看到错误,指出 TypeError: _this.setState is not a function
。
我尝试了不同的方法来修复它,但仍然没有。
const NewItemForm = props => (
<Form onSubmit={props.send_form}>
<Form.Group>
<TextArea
placeholder='Name your first item here'
name='item_msg'
onChange={e => this.setState({ item_msg: e.target.value })} />
<Form.Button primary content='Create Item' />
</Form.Group>
</Form>
)
class App extends Component {
constructor () {
super();
this.state = {
item_msg: ''
}
}
handleSubmit(e){
e.preventDefault();
console.log(this.state.item_msg);
}
render() {
return (
<div className="App">
<MainHeaderr />
<Container>
<NewItemForm send_form={this.handleSubmit.bind(this)} />
</Container>
</div>
);
}
}
export default App;
最佳答案
功能组件没有生命周期方法和... state
:)
const NewItemForm = props => (
<Form onSubmit={props.send_form}>
<Form.Group>
<TextArea
placeholder='Name your first item here'
name='item_msg'
onChange={e => this.setState({ item_msg: e.target.value })} />
<Form.Button primary content='Create Item' />
</Form.Group>
</Form>
)
这行不通:
onChange={e => this.setState({ item_msg: e.target.value })} />
你需要的是传递回调:
const NewItemForm = props => (
<Form onSubmit={props.send_form}>
<Form.Group>
<TextArea
placeholder='Name your first item here'
name='item_msg'
onChange={props.onInputChange} />
<Form.Button primary content='Create Item' />
</Form.Group>
</Form>
)
class App extends Component {
constructor () {
super();
this.state = {
item_msg: ''
}
this.handleSubmit = this.handleSubmit.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
}
handleSubmit(e){
e.preventDefault();
console.log(this.state.item_msg);
}
handleInputChange(e) {
this.setState({ item_msg: e.target.value })
}
render() {
return (
<div className="App">
<MainHeaderr />
<Container>
<NewItemForm send_form={this.handleSubmit} onInputChange={this.handleInputChange} />
</Container>
</div>
);
}
}
我知道你来自哪里,但是 NewItemForm
将被转译为 React Element 所以 this
将引用该元素,而不是 App
成分。
关于javascript - React - TypeError : _this. setState 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49368464/
我写了以下代码片段: export class Kurse extends React.Component { constructor(props) { super(props);
这个问题在这里已经有了答案: How to access the correct `this` inside a callback (13 个答案) 关闭 7 年前。 我一直在搜索,但找不到以 ES
我在使用 TypeScript 将 $(this) 替换为 $(_this) 并破坏代码时遇到问题,因为 test 指的是 窗口。 $(".class").click(() => { var
这是一个非常人为的例子,但假设我们在类函数的某处创建了一个变量 _this。 class Person { constructor (public name : string) {} chan
我尝试在 TypeScript 中为 String.Prototype 定义一些属性: Object.defineProperty(String.prototype, 'test', { va
我创建了一个Vue组件,调用imageUpload并将属性作为v模型传递 并且在imgeUpload组件内 我有这个代码 upload:(e)=>{ const files = e.tar
这两种说法有什么区别jquery 中的 $(this) 和 $(_this)。 最佳答案 $(this) 是上下文的当前元素选择器,但 $(_this) 是普通变量选择器。 例如: $('p').on
我正在尝试将最初用 JavaScript 编写的用于捕获视频的实用程序转换为 Angular。 我收到错误_this.video 未定义。原包在这里:https://developer.mozilla
您好,我正在尝试用 JavaScript 创建俄罗斯方 block 。我有一个带有名为 data 的变量的 map 对象。当我从对象调用函数时,“this”不再是对象,而是我认为的本地函数。我尝试在对
我收到以下错误消息:在使用 karma/ Jasmine 的Angular 6单元测试中,this.handler.handle不是函数。当我在项目文件夹中键入“ng test”命令时,在命令行中会出
我正在使用 Gatsby 构建一个网站,现在我正在创建一个 blog-post.js 文件,该文件通过来自 Markdown 文件的 graphql 数据进行查询并为每个文件创建页面。它还应包括页眉、
我有一个可以像这样简化的类: Captcha = function(el) { this.el = $(el); _this = this; captchas.push(thi
我正在尝试整体改进我的代码,尽管以下代码确实有效,但我想避免使用 _this(对我来说这是一种笨拙的方式),并开始使用 .call/.apply 或 .bind 来在以下示例中设置此上下文。 这是 j
我在下面的函数调用中收到一个错误,指出 this.albumDetails 对于特定行是未定义的,即使所有其他 this 调用都工作正常。 错误是:TypeError: _this.albumDeta
我是 React Native 的新手,请帮助我解决这个问题:TypeError: _props.goToScreen 不是一个函数,请帮助我提前致谢 Profile.js 我想单击 GotoHome
我试图通过转换现有应用程序来了解 ES6 和 Angular。毫不奇怪,挑战就是这个! let _$localStorage 来自 ( Injected dependencies not access
在我的 LoginProvider 中,我使用了一个函数来执行登录并将创建的 session 作为 promise 返回。 @Injectable() export class LoginProvid
我对工具提示进行了引导修改。 并使用 webpack/babel 处理我的 js 我的代码的简化可以是: $('[data-toggle="tooltip"]').tooltip({
尝试使用 angular2-flash-messages 并得到控制台错误 _this.flashMessagesService.show is not a function 按照 https://g
在下面的示例中我收到此错误 TypeError: _this is undefined 我尝试了很多方法,但找不到解决该错误的方法,我该如何解决? component_for_home_page.js
我是一名优秀的程序员,十分优秀!