gpt4 book ai didi

javascript - 在 es6 中使用 onChange

转载 作者:行者123 更新时间:2023-11-29 16:43:45 24 4
gpt4 key购买 nike

我只是在学习。将代码从 ES5 转换为 ES6 时,我的下拉菜单的“onChange”事件不起作用,而且我不知道问题出在哪里。我浏览了“可能有你答案的问题”,但什么也没有。我的应用程序可以编译,但控制台出现错误。我怀疑是 Child.js 的“select”标签中的“onChange”属性。我将不胜感激提供的任何帮助。这是我的代码:


index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Parent from './Parent';

ReactDOM.render(
<Parent />,
document.getElementById('root')
);

Parent.js

import React from 'react';
import Child from './Child';
import Sibling from './Sibling';

class Parent extends React.Component {

constructor(props) {
super(props);
this.state = {name: 'Frarthur'};
this.changeName = this.changeName.bind(this);
}

changeName(newName) {
this.setState({name: newName});
}

render() {
return (
<div>
<Child onChange={this.changeName} />
<Sibling name={this.state.name} />
</div>
);
}
}

export default Parent;

Child.js

import React from 'react';

class Child extends React.Component {

handleChange(e){
let name = e.target.value;
this.props.onChange(name);
}

render(){
return (
<div>
<select
id="great-names"
onChange={() => this.handleChange()}>

<option value="Frarthur">Frarthur</option>
<option value="Gromulus">Gromulus</option>
<option value="Thinkpiece">Thinkpiece</option>

</select>
</div>
);
}
}

export default Child;

Sibling.js

import React from 'react';

class Sibling extends React.Component {

render(){
let name = this.props.name;

return (
<div>
<h1>Hey, my name is {name}!</h1>
<h2>Don't you think {name} is the prettiest name ever?</h2>
<h2>Sure am glad my parents picked {name}!</h2>
</div>
);
}
}

export default Sibling;

控制台

Uncaught TypeError: Cannot read property 'indexOf' of null
at content.js:4186
Child.js:6Uncaught TypeError: Cannot read property 'target' of undefined
at Child.handleChange (http://localhost:3000/static/js/bundle.js:32622:20)
at onChange (http://localhost:3000/static/js/bundle.js:32644:30)
at Object.executeOnChange (http://localhost:3000/static/js/bundle.js:24438:35)
at ReactDOMComponent._handleChange (http://localhost:3000/static/js/bundle.js:24795:39)
at Object.ReactErrorUtils.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16910:17)
at executeDispatch (http://localhost:3000/static/js/bundle.js:16692:22)
at Object.executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:16715:6)
at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16103:23)
at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16114:11)
at Array.forEach (native)
Child.js:6Uncaught TypeError: Cannot read property 'target' of undefined
at Child.handleChange (http://localhost:3000/static/js/bundle.js:32622:20)
at onChange (http://localhost:3000/static/js/bundle.js:32644:30)
at Object.executeOnChange (http://localhost:3000/static/js/bundle.js:24438:35)
at ReactDOMComponent._handleChange (http://localhost:3000/static/js/bundle.js:24795:39)
at Object.ReactErrorUtils.invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:16910:17)
at executeDispatch (http://localhost:3000/static/js/bundle.js:16692:22)
at Object.executeDispatchesInOrder (http://localhost:3000/static/js/bundle.js:16715:6)
at executeDispatchesAndRelease (http://localhost:3000/static/js/bundle.js:16103:23)
at executeDispatchesAndReleaseTopLevel (http://localhost:3000/static/js/bundle.js:16114:11)
at Array.forEach (native)

最佳答案

您忘记将事件传递给handleChange函数,请使用:

onChange={(e) => this.handleChange(e)}

或者这个:

onChange={this.handleChange.bind(this)}

关于javascript - 在 es6 中使用 onChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43239768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com