gpt4 book ai didi

javascript - redux 不更新 reducer 后的 View

转载 作者:行者123 更新时间:2023-11-30 08:32:10 28 4
gpt4 key购买 nike

已解决:字段表单中的默认值与我想象的不同。

我不知道我在做什么 [化学狗图片在这里]。

我的组件称为 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));
}

渲染:

   return(
<form onSubmit={this.handleSubmit}>
<label htmlFor="for_owner">Eingentümer (owner):</label>
<input name="owner" defaultValue={this.props.appo.owner} />
</form>
);

this.props.appo 实际上已更新但 View 未更改:

enter image description here

更新

非常感谢你们的帮助,从一开始我的 reducer 就是:

    case RECEIVE_ONE_APPO
Object.assign({}, state, {
appArrayPop: action.appArrayPop
});

为了降低复杂性,我删除了数组选项,用一个简单的字符串替换它,而且,我删除了所有其他的 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
});

然后我注意到一些奇怪的事情:

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>
);

结果:

enter image description here

因此,{this.props.eigentumer} 在位置 1 和 2 中更新,但在defaultValue={this.props.eigentumer} 这就是 View 未更新的原因。现在我需要 read this知道为什么会这样。

非常感谢您的帮助。

最佳答案

如何更改 appoArrayProp?如果您正在处理同一个对象,并且只是删除或添加键,则 nextPropsthis.props 指向同一个对象,并且它们将始终相等.是的,这并不直观,但事实就是如此。

假设您有对象 appoArrayProp:

appoArrayProp = {
"foo": 1,
"bar": 2
}

如果您通过简单地添加或删除属性来更改它,nextProps.appoArrayPropthis.props.appoArrayProp 将指向同一个对象:

appoArrayProp = {
"foo": 1,
"bar": 2,
"newProp": 3
}

和 Object.keys().length 比较将始终返回 true。解决方案是创建一个新对象,复制当前 appoArrayProp 的属性,在新对象中添加或删除键,然后将其作为属性传递:

appoArrayProp = Object.assign({}, appoArrayProp)
/* Do changes to appoArrayProp and render component */

关于javascript - redux 不更新 reducer 后的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36070616/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com