[英]The '.onChange' method doesn't get the newest info of 'Input' in React.js
import reqwest from './public/reqwest.js'
const PAGE_SIZE = 10
class App extends Component {
constructor() {
super()
this.state = {
page: 1,
arr: []
}
}
singleInfo(page) {
reqwest({
url: 'https://cnodejs.org/api/v1/topics',
data: {
limit: PAGE_SIZE,
page: this.state.page
},
success: function (data) {
this.setState({
arr: data.data
})
}.bind(this)
})
}
changeState(newState) {
this.setState(newState)
this.singleInfo(newState.page)
}
render() {
return (
<div>
<Menu val={this.state.page} changeParentState={(state) => this.changeState(state)} />
<List arr={this.state.arr} />
</div>
);
}
}
class Menu extends Component {
handleChange(event) {
if(event.target.value) {
this.props.changeParentState({
page: event.target.value
})
}
}
render() {
console.log(this)
return <input type="text" defaultValue={this.props.val} onChange={(event) => this.handleChange(event)} />
}
}
class List extends Component {
render() {
return <ul>
{
this.props.arr.map((ele) => {
return (
<li key={ ele.id }>
<p className="title">{ ele.title }</p>
<p className="date">{ ele.create_at }</p>
<p className="author">{ ele.author.loginname }</p>
</li>
)
})
}
</ul>
}
}
I can't get the current value of the input
by onChange
in Menu module
. 我无法通过
Menu module
的onChange
获取input
的当前值。
In my code, the App has two child components - List
& Menu
. 在我的代码中,该应用程序有两个子组件-
List
和Menu
。 You can input the page in Menu
component, so it will send Ajax() to get the info of the page. 您可以在
Menu
组件中输入页面,这样它将发送Ajax()以获得页面信息。 But the truth is: After I change the value of input like 1 -> 10, the ajax get the value of 1. 但事实是:我将输入的值更改为1-> 10后,ajax的值为1。
Before this, I know the difference between keyup
or keydown
and keypress
. 在此之前,我知道
keyup
或keydown
和keypress
之间的区别。 They have the difference cross browser. 它们具有跨浏览器的区别。 But I just want get the current value of the input By React.js .
但是我只想通过React.js获取输入的当前值 。
First, change: 首先,更改:
<input type="text" defaultValue={this.props.val} onChange={(event) => this.handleChange(event)} />
To: 至:
<input type="text" value={this.props.val} onChange={(event) => this.handleChange(event)} />
so that your input will update to the correct value on re-render. 这样您的输入将在重新渲染时更新为正确的值。
Second, remember that setState
is often asynchronous. 其次,请记住
setState
通常是异步的。 So do not expect the state to be changed right after calling setState
. 因此,不要期望在调用
setState
之后立即更改状态。
Your changeState
method is good in this respect since it passes newState
to singlePageRequest
. 在这方面,您的
changeState
方法是好的,因为它将newState
传递给singlePageRequest
。 However singlePageRequest
does not use the supplied value and instead uses this.state.page
. 但是
singlePageRequest
不使用提供的值,而是使用this.state.page
。 Change it to use the supplied value and you should be OK: 更改它以使用提供的值,您应该可以:
singleInfo(page) {
reqwest({
url: 'https://cnodejs.org/api/v1/topics',
data: {
limit: PAGE_SIZE,
page: page
},
success: function (data) {
this.setState({
arr: data.data
})
}.bind(this)
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.