reactjs - 当切换 div 时输入值保持不变的原因
问题描述
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
inputvalue : '',
allval: '',
bool: false
}
}
onChangeOfInput =(name,value) =>{
this.setState({
[name]: value
});
}
clickHandler = () =>{
console.log(this.state);
this.setState({
bool: !this.state.bool
});
}
render() {
return (
<div className="hello">
{this.state.bool
? <div>1:<Input placeholder="Title 1" name="Title1" onChangeOfInput={this.onChangeOfInput} />
2:<Input placeholder="Title 2" name="Title2" onChangeOfInput={this.onChangeOfInput} /> </div>
:
<div>
3:<Input placeholder="Title 3" name="Title3" onChangeOfInput={this.onChangeOfInput}/>
4:<Input placeholder="Title 4" name="Title4" onChangeOfInput={this.onChangeOfInput}/>
</div>
}
<button onClick={this.clickHandler}>Change</button>
</div>
)
}
}
class Input extends React.Component {
constructor(props) {
super(props)
this.state = {
inputvalue: ''
}
}
handleChange(e) {
this.setState({
inputvalue: e.target.value
});
this.props.onChangeOfInput(this.props.name,e.target.value)
}
render() {
return (
<input
type="text"
placeholder={this.props.placeholder}
value={this.state.inputvalue}
onChange={this.handleChange.bind(this)}
/>
)
}
}
ReactDOM.render(<App />, document.querySelector("#app"))
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
li {
margin: 8px 0;
}
h2 {
font-weight: bold;
margin-bottom: 15px;
}
.done {
color: rgba(0, 0, 0, 0.3);
text-decoration: line-through;
}
input {
margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
当我输入一些值时,我DIVs
通过单击Change
按钮来切换。输入值保留在该字段上。如何确保输入值字段只显示自己的值?谢谢
解决方案
而不是在子组件中使用状态,即Input
组件。您可以在父组件中添加状态,即App
每个输入字段的组件。
constructor(props) {
super(props)
this.state = {
inputvalue : '',
allval: '',
bool: false,
Title1: '',
Title2: '',
Title3: '',
Title4: ''
}
}
App
之后,您可以通过将value
props 传递给组件来将每个输入字段值映射到组件的特定状态Input
。
IE<Input placeholder="Title 1" name="Title1" value={this.state.Title1} onChangeOfInput={this.onChangeOfInput} />
所以你可以通过在每个组件中添加value
道具来更新渲染方法Input
render() {
return (
<div className="hello">
{this.state.bool
? <div>1:<Input placeholder="Title 1" name="Title1" value={this.state.Title1} onChangeOfInput={this.onChangeOfInput} />
2:<Input placeholder="Title 2" name="Title2" value={this.state.Title2} onChangeOfInput={this.onChangeOfInput} /> </div>
:
<div>
3:<Input placeholder="Title 3" name="Title3" value={this.state.Title3} onChangeOfInput={this.onChangeOfInput}/>
4:<Input placeholder="Title 4" name="Title4" value={this.state.Title4} onChangeOfInput={this.onChangeOfInput}/>
</div>
}
<button onClick={this.clickHandler}>Change</button>
</div>
)
}
最后你可以使用组件中的value
道具Input
<input
type="text"
placeholder={this.props.placeholder}
value={this.props.value}
onChange={this.handleChange.bind(this)}
/>
所以更新你的Input
组件
class Input extends React.Component {
handleChange(e) {
this.props.onChangeOfInput(this.props.name,e.target.value)
}
render() {
return (
<input
type="text"
placeholder={this.props.placeholder}
value={this.props.value}
onChange={this.handleChange.bind(this)}
/>
)
}
}
通过这种方式,您可以确保输入值字段仅显示其自己的值
推荐阅读
- azure - Azure Web 应用程序随机重启 - 任何人都可以扩展此建议吗?
- python - ModuleNotFoundError:没有名为“redsquare.moduloMatriculas”的模块”DJANGO
- windows - Powershell - 根据csv用户列表导出用户成员..组
- c# - 使用需要 T1 的 T2 的构造函数从 T1 列表创建 T2 列表
- css - CSS 打印重复内容
- c++ - 如何使用 python 读取 c++ 程序在标准输出中存储的字节序列?
- git - 为什么我需要拉取请求?
- python - Python:在 DataFrame 上转换时间单位
- javascript - D3.js 网络图
- ios - iphone X的应用程序全屏问题