Reactjs 如何使按钮在输入时工作(反应)
我想要一个“添加”按钮。下面是React.js代码,我认为这是实现我想要的逻辑的一种方法,但不幸的是它不起作用 我收到此错误的消息:Reactjs 如何使按钮在输入时工作(反应),reactjs,Reactjs,我想要一个“添加”按钮。下面是React.js代码,我认为这是实现我想要的逻辑的一种方法,但不幸的是它不起作用 我收到此错误的消息: bundle.js:47 Uncaught Error: Cannot find module "./src/index.js" at webpackMissingModule at Object.<anonymous> at __webpack_require__ bundle.js:47未捕获错误:找不到模块“/src
bundle.js:47 Uncaught Error: Cannot find module "./src/index.js"
at webpackMissingModule at Object.<anonymous>
at __webpack_require__
bundle.js:47未捕获错误:找不到模块“/src/index.js”
在webpackMissingModule对象处。
在需要的网页上
我如何解决这个问题
import React from "react"
import ReactDOM from "react-dom"
import SearchBar from "./components/search_bar"
const API_KEY = "TheRealAPIKeyWouldGoHere"
const App = () => {
handleChange(value){
this.setState({
value: event.target.value
});
}
return ( <div>
<SearchBar onChange={this.handleChange}/>
</div>
)
}
ReactDOM.render(<App />, document.querySelector(".container"))
从“React”导入React
从“react dom”导入react dom
从“/components/search_bar”导入搜索栏
const API_KEY=“TheRealAPIKeyWouldGoHere”
常量应用=()=>{
handleChange(值){
这是我的国家({
值:event.target.value
});
}
报税表(
)
}
ReactDOM.render(,document.querySelector(“.container”))
这是我的组件。我已将按钮指定给输入,但我不知道如何使其工作
import React, { Component } from "react"
class SearchBar extends Component {
constructor(props){
super(props)
this.state = {term: ""}
}
handleChange(evt){
this.props.onChange(value);
}
render () {
return <div>
<button onClick={this.handleChange}>Search</button>
<input ref="inputName" onChange= { event => console.log(event.target.value)} />
</div>
}
}
export default SearchBar
import React,{Component}来自“React”
类搜索栏扩展组件{
建造师(道具){
超级(道具)
this.state={term:}
}
手柄更换(evt){
this.props.onChange(值);
}
渲染(){
返回
搜寻
console.log(event.target.value)}/>
}
}
导出默认搜索栏
在您的组件中:
import React,{Component}来自“React”
类搜索栏扩展组件{
建造师(道具){
超级(道具)
this.state={term:'}
this.handleChange=this.handleChange.bind(this)
this.handleSubmit=this.handleSubmit.bind(this)
}
手变(活动){
this.setState({value:event.target.term})
}
handleSubmit(事件){
console.log(event.target.value)
event.preventDefault()
}
render(){
返回(
搜寻
)
}
}
导出默认搜索栏
您没有列出您正在联系的API,所以这次可能没问题,但请小心将API密钥粘贴到公共论坛中。看起来像是网页包配置错误,而不是React问题您的第一个类是否不在“src/index.js”中?我认为webpack被配置为将该类视为您的入口点。这是编译错误,不是吗?