aar*_*rio 1 javascript reactjs redux redux-thunk
求助:字段形式的defaultValue不是我想象的那样.
我不知道我在做什么[化学狗图片在这里].
我的组件称为loadForm,并通过thunk操作从API调用加载数据:
class loadForm extends Component {
constructor(props) {
super(props);
}
/* Loads data to the form */
componentDidMount() {
let action = getAppoThunk(this.props.routeParams.id);
this.props.dispatch(action);
console.log('DidMount appoArray>>'+ JSON.stringify(this.props.appo));
}
componentWillReceiveProps(nextProps) {
if (Object.keys(nextProps.appoArrayProp).length != Object.keys(this.props.appoArrayProp).length ) {
console.log('WWWW NOT THE SAME nextProps >>'+ JSON.stringify(nextProps.appo));
console.log('WWWW NOT THE SAME thisProps >>' + JSON.stringify(this.props.appo));
let action = getAppoThunk(this.props.routeParams.id);
this.props.dispatch(action); // thunk middleware dispatch
} else {
console.log('ARE THE SAME this.props.appo>>' + JSON.stringify(this.props.appo));
}
Run Code Online (Sandbox Code Playgroud)
}
渲染:
return(
<form onSubmit={this.handleSubmit}>
<label htmlFor="for_owner">Eingentümer (owner):</label>
<input name="owner" defaultValue={this.props.appo.owner} />
</form>
);
Run Code Online (Sandbox Code Playgroud)
this.props.appo实际上已更新,但视图不会更改:
UPDATE
非常感谢你们的帮助,从一开始我的减速机是:
case RECEIVE_ONE_APPO
Object.assign({}, state, {
appArrayPop: action.appArrayPop
});
Run Code Online (Sandbox Code Playgroud)
为了降低复杂性,我删除了数组选项,用一个简单的字符串替换它,而且,我删除了所有其他的reducer,所以我的reducer现在只是:
const initialState = {
eigentumer: 'initial' // owner
};
const appo_rdcer = (state = initialState, action) => {
switch (action.type){
case RECEIVE_ONE_APPO:
return Object.assign({}, state, {
eigentumer: action.eigentumer
});
Run Code Online (Sandbox Code Playgroud)
然后我注意到一些奇怪的东西:
return (
<div id="responsive" className="modal hide fade" tabIndex="-1" >
<Modal aria-labelledby='modal-label'
style={modalStyle}
backdropStyle={backdropStyle}
show={this.state.showModal}
>
<Modal.Header>
<Modal.Title>Modal Überschrift 1 --> {this.props.eigentumer} </Modal.Title>
</Modal.Header>
<Modal.Body>
<form onSubmit={this.handleSubmit}>
<label htmlFor="for_owner">Eigentümer: 2 --> {this.props.eigentumer} </label>
<input className="form-control" id="for_owner" name="owner" defaultValue={this.props.eigentumer} />
</form>
</Modal.Body>
<Modal.Footer>
<Button onClick={() => browserHistory.push('/appointments')}>Close</Button>
<Button bsStyle="primary">Änderungen speichern</Button>
</Modal.Footer>
</Modal>
</div>
);
Run Code Online (Sandbox Code Playgroud)
结果:
因此,{this.props.eigentumer}在位置1和2中更新,但不在 defaultValue = {this.props.eigentumer}中更新,这就是视图未更新的原因.现在我需要阅读这个以了解为什么会发生这种情况.
非常感谢你的帮助.
你怎么改变appoArrayProp?如果您正在处理同一个对象,只是删除或添加键,nextProps并且this.props指向同一个对象,那么这些对象将始终相等.是的,它不直观,但它就是这样.
假设您有对象appoArrayProp:
appoArrayProp = {
"foo": 1,
"bar": 2
}
Run Code Online (Sandbox Code Playgroud)
如果通过简单地添加或删除属性来更改它,nextProps.appoArrayProp并且this.props.appoArrayProp都指向同一个对象:
appoArrayProp = {
"foo": 1,
"bar": 2,
"newProp": 3
}
Run Code Online (Sandbox Code Playgroud)
并且Object.keys().长度比较将始终返回true.解决方案是创建一个新对象,复制当前appoArrayProp的属性,从新对象添加或删除键,然后将其作为属性传递:
appoArrayProp = Object.assign({}, appoArrayProp)
/* Do changes to appoArrayProp and render component */
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1212 次 |
| 最近记录: |