我想编写一个工具来迭代学校类(class)的学生。 所以我在 react 组件中有一个显示值的文本字段:
<input className="form-control" onChange={this.handleInputChange} value={this.props.activePupil.name}></input>
如果我将它的值绑定(bind)到瞳孔名称(它由我的 Redux 存储作为 Prop 提供给我的组件),每当存储中的值发生变化时,它总是会向我显示正确的值。
但是,我还希望用户能够更改学生的姓名(如果不准确)。如果输入字段的值与 Prop 相关联,则此方法不起作用。
所以我想我应该将它绑定(bind)到一些本地状态,并且在构造函数中我这样做:
this.state = {
inputField: "No Pupil selected yet."
};
...每当用户编辑文本字段时,都会在 handleInputChange 方法中进行处理,如下所示:
this.setState({
inputField: evt.target.value
});
这一切都工作正常,但现在当我想迭代学校类(class)时,包含当前或事件学生的 reducer 是正确的(console.log 显示正确的值),但输入字段中显示的值总是落后一队。
为什么会这样?
编辑:完整代码:
// Import React
import React from 'react';
class PupilViewer extends React.Component{
constructor(props) {
super(props);
this.state = {
inputField: "No Pupil selected yet."
};
this.handleInputChange = this.handleInputChange.bind(this);
this.onSubmit = this.onSubmit.bind(this);
}
handleInputChange(evt) {
this.setState({
inputField: evt.target.value
});
}
onSubmit(e) {
e.preventDefault();
this.props.nextPupil();
this.state.inputField = this.props.activePupil.name;
}
render() {
// Return JSX via render()
return (
<div className="">
<h1>Pupil View</h1>
<input className="form-control" onChange={this.handleInputChange} value={this.state.inputField}></input>
<button className="btn btn-large btn-positive" onClick={this.onSubmit}>Next</button>
</div>
);
}
}
// Export Search
export default PupilViewer
上面的组件从容器中给出以下参数:
<PupilViewer nextPupil={this.props.nextPupil} pupilList={this.props.pupils} activePupil={this.props.activePupil} saveChanges={this.props.saveChanges} updateActivePupil={this.props.updateActivePupil}/>
我需要在这里指出,nextPupil、saveChanges 和 updateActivePupil 是我在这里传递的函数。 我有两个 reducer 。一个包含一个带有所有瞳孔的物体,另一个包含事件瞳孔:
主动式瞳孔缩小器:
export default function (state={id: -1, name:"Default Pupil Name in State"}, action) {
switch (action.type) {
case "UPDATE_ACTIVE":
let newState = action.payload;
return newState;
case "GET_ACTIVE":
return state;
default:
return state;
}
}
我的 reducer 和所有学生:
//my default array
let default_pupils = [{
id: 0,
name: "Matthew",
//there are more attributes here
},
{
//and there are more pupils here
}];
export default function (state=default_pupils, action) {
switch (action.type) {
case "SAVE_CHANGES":
let newState = [...state];
newState[2] = {...newState[2], name: action.payload.name };
return newState;
default:
return state;
}
}
最后,这是我的行动:
var activePupilCount = 0;
export const getActivePupil = () => {
return {
type: "GET_ACTIVE"
};
}
export const updateActivePupil = (pupil) => {
console.log("UPDATE:" + pupil.name)
return {
type: "UPDATE_ACTIVE",
payload: pupil
};
}
export const nextActivePupil = () => {
return (dispatch, getState) => {
const {activePupil, pupilList} = getState();
activePupilCount++;
console.log("COUNT:"+ activePupilCount);
const nextIndex = (activePupilCount) % pupilList.length;
const nextActive = pupilList[nextIndex];
dispatch({ type: "UPDATE_ACTIVE", payload: nextActive });
}
};
export const saveChanges = (pupil) => {
return {
type: "SAVE_CHANGES",
payload: pupil
}
};
最佳答案
不确定这是否能解决所有问题并使其按预期工作,但你永远不应该这样做
this.state.inputField = this.props.activePupil.name;
相反,这样做
this.setState({ inputField: this.props.activePupil.name })
关于javascript - 尽管 console.log 显示正确的值,但 Textfield 不会对Reducer使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43009574/