Javascript 未在输入字段React redux或ReactJS中获取所需值
Javascript 未在输入字段React redux或ReactJS中获取所需值,javascript,reactjs,react-redux,Javascript,Reactjs,React Redux,import React,{Component}来自'React'; 让uz=require('lodash'); 从“redux”导入{bindActionCreators}; 从'react redux'导入{connect}; 从“../../actions/”导入{fetchedZonesEdit}; 类InfoRow扩展了组件{ 建造师(道具){ 超级(道具); this.handleInputChange=this.handleInputChange.bind(this); } ha
import React,{Component}来自'React';
让uz=require('lodash');
从“redux”导入{bindActionCreators};
从'react redux'导入{connect};
从“../../actions/”导入{fetchedZonesEdit};
类InfoRow扩展了组件{
建造师(道具){
超级(道具);
this.handleInputChange=this.handleInputChange.bind(this);
}
handleInputChange(事件){
这是我的国家({
[event.target.name]:event.target.value
});
}
render(){
返回(
{this.props.zone}
{this.props.zoneValue}
)
}
}
类ZoneDetailsEdit扩展组件{
render(){
常量行=[];
设a=this.props.ezn;
Object.keys(this.props.ezn).map((keyName,keyIndex)=>{
返回行。push()
});
返回(
{rows}
拯救
)
}
}
类ZoneDetailEditComponent扩展了组件{
组件将装入=()=>{
this.props.fetchedZonesEdit(this.props.location.query.id);
};
render(){
返回(
编辑标签信息
{this.props.ezn!=null?
:
加载
}
)
}
}
函数MapStateTops(状态){
返回{
ezn:state.zones
}
}
功能匹配DispatchToprops(调度){
返回bindActionCreators({fetchedZonesEdit:fetchedZonesEdit},dispatch);
}
导出默认连接(mapStateToProps、matchDispatchToProps)(ZoneDetailEditComponent)代码>defaultValue
只能在上使用
不要使用defaultValue
,而是为商店中的名称指定一个默认值
另外,如果您正在使用redux,请不要使用this.setState
。您正在将新值分配给从未阅读过的此.state.zone
。defaultValue
应仅在上使用
不要使用defaultValue
,而是为商店中的名称指定一个默认值
另外,如果您正在使用redux,请不要使用this.setState
。您正在将新值分配给从未阅读过的this.state.zone
。由于您使用的是受控组件,因此无需使用defaultValue,分配传递给该值的道具就足够了
同样使用redux,更好的做法是将UI状态存储在localState中,并将所有其他状态存储在redux存储中
要做到这一点,您需要调度一个操作,在将值传递给最顶层的父级之后更新相应的reducer
另外,您没有将任何道具作为name
传递给InfoRow
组件,并且由于defaultValue仅在创建时呈现,因此您看不到更新
您的代码必须看起来像
import React, { Component } from 'react';
let _ = require('lodash');
import {bindActionCreators} from "redux";
import {connect} from 'react-redux';
import {fetchedZonesEdit} from '../../actions/';
class InfoRow extends Component {
constructor(props){
super(props);
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
this.props.handleChange(this.props.zone, event.target.value);
}
render() {
return (
<tr>
<td>
{this.props.zone}
</td>
<td>{this.props.zoneValue}
<input type="text"
className="form-control"
value={this.props.zoneValue}
name={this.props.zone}
onChange={this.handleInputChange}
/>
</td>
</tr>
)
}
}
class ZoneDetailsEdit extends Component {
handleChange(zone, value) {
//pass it to the parent and then fire an action from there to update this value in the store
}
render() {
const rows = [];
let a = this.props.ezn;
Object.keys(this.props.ezn).map((keyName, keyIndex) =>{
return rows.push(<InfoRow zone={keyName} zoneValue={a[keyName].toString()} handleChange={()=> this.handleChange}key={keyIndex}/>)
});
return (
<div className="col-md-6">
<div className="">
<table className="table table-clear">
<tbody>
{rows}
</tbody>
</table>
</div>
<div className="row px-1" >
<div className="px-2">
<button className="btn btn-sm btn-info">Save</button>
</div></div>
</div>
)
}
}
import React,{Component}来自'React';
让uz=require('lodash');
从“redux”导入{bindActionCreators};
从'react redux'导入{connect};
从“../../actions/”导入{fetchedZonesEdit};
类InfoRow扩展了组件{
建造师(道具){
超级(道具);
this.handleInputChange=this.handleInputChange.bind(this);
}
handleInputChange(事件){
this.props.handleChange(this.props.zone、event.target.value);
}
render(){
返回(
{this.props.zone}
{this.props.zoneValue}
)
}
}
类ZoneDetailsEdit扩展组件{
handleChange(区域、值){
//将其传递给父级,然后从父级启动操作以更新存储中的此值
}
render(){
常量行=[];
设a=this.props.ezn;
Object.keys(this.props.ezn).map((keyName,keyIndex)=>{
返回rows.push(this.handleChange}key={keyIndex}/>)
});
返回(
{rows}
拯救
)
}
}
此外,您无需使用箭头绑定lifeCycle函数,因为您使用的是受控组件,不需要使用defaultValue,分配传递给该值的道具就足够了
同样使用redux,更好的做法是将UI状态存储在localState中,并将所有其他状态存储在redux存储中
要做到这一点,您需要调度一个操作,在将值传递给最顶层的父级之后更新相应的reducer
另外,您没有将任何道具作为name
传递给InfoRow
组件,并且由于defaultValue仅在创建时呈现,因此您看不到更新
您的代码必须看起来像
import React, { Component } from 'react';
let _ = require('lodash');
import {bindActionCreators} from "redux";
import {connect} from 'react-redux';
import {fetchedZonesEdit} from '../../actions/';
class InfoRow extends Component {
constructor(props){
super(props);
this.handleInputChange = this.handleInputChange.bind(this);
}
handleInputChange(event) {
this.props.handleChange(this.props.zone, event.target.value);
}
render() {
return (
<tr>
<td>
{this.props.zone}
</td>
<td>{this.props.zoneValue}
<input type="text"
className="form-control"
value={this.props.zoneValue}
name={this.props.zone}
onChange={this.handleInputChange}
/>
</td>
</tr>
)
}
}
class ZoneDetailsEdit extends Component {
handleChange(zone, value) {
//pass it to the parent and then fire an action from there to update this value in the store
}
render() {
const rows = [];
let a = this.props.ezn;
Object.keys(this.props.ezn).map((keyName, keyIndex) =>{
return rows.push(<InfoRow zone={keyName} zoneValue={a[keyName].toString()} handleChange={()=> this.handleChange}key={keyIndex}/>)
});
return (
<div className="col-md-6">
<div className="">
<table className="table table-clear">
<tbody>
{rows}
</tbody>
</table>
</div>
<div className="row px-1" >
<div className="px-2">
<button className="btn btn-sm btn-info">Save</button>
</div></div>
</div>
)
}
}
import React,{Component}来自'React';
让uz=require('lodash');
从“redux”导入{bindActionCreators};
从'react redux'导入{connect};
从“../../actions/”导入{fetchedZonesEdit};
类InfoRow扩展了组件{
建造师(道具){
超级(道具);
this.handleInputChange=this.handleInputChange.bind(this);
}
handleInputChange(事件){
this.props.handleChange(this.props.zone、event.target.value);
}
render(){
返回(
{this.props.zone}
{this.props.zone