你在评论中说你被一个刚离开公司的开发人员留下了这段代码。恐怕他们给你留下了违反React两条规则的代码:-)
-
不能直接修改状态,包括
this.state
currentFiles.splice(index, 1)
.
-
您正在基于现有状态设置新状态,但不使用的回调形式
setState
.
// Use the callback form that receives the up-to-date state as a parameter.
this.setState(
({openFiles}) => {
var index = openFiles.findIndex((f: IFileModel) => f.fileId == fileId)
// (Do you need an `if (index !== -1)` check here?)
// Create a *new* array without the entry
var currentFiles = [...openFiles.slice(0, index), ...openFiles.slice(index+1)];
// Return the new state
return {
mode: "gallery",
openFiles: currentFiles
};
},
() => console.log(this.state.mode)
);
the state docs
.
实例:
class Example extends React.Component {
constructor(...args) {
super(...args);
this.removeFileOnClick = this.removeFileOnClick.bind(this);
this.state = {
mode: "main",
openFiles: [
{fileId: 1, name: "File 1"},
{fileId: 2, name: "File 2"},
{fileId: 3, name: "File 3"},
{fileId: 4, name: "File 4"},
{fileId: 5, name: "File 5"}
]
};
}
removeFileOnClick(e) {
const fileId = e.currentTarget.getAttribute("data-id");
this.setState(
({openFiles}) => {
var index = openFiles.findIndex((f) => f.fileId == fileId)
// (Do you need an `if (index !== -1)` check here?)
// Create a *new* array without the entry
var currentFiles = [...openFiles.slice(0, index), ...openFiles.slice(index+1)];
// Return the new state
return {
mode: "gallery",
openFiles: currentFiles
};
},
() => console.log(this.state.mode)
);
}
render() {
return (
<div>
Mode: {this.state.mode}
<div>
OpenFiles ({this.state.openFiles.length}):
<div>{this.state.openFiles.map(file =>
<div><button data-id={file.fileId} onClick={this.removeFileOnClick}>X</button>{file.name}</div>
)}</div>
</div>
</div>
);
}
}
ReactDOM.render(
<Example />,
document.getElementById("root")
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.4.2/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.4.2/umd/react-dom.production.min.js"></script>
旁注:如果您不喜欢这里的双排列:
var currentFiles = [...openFiles.slice(0, index), ...openFiles.slice(index+1)];
你可以这样做:
var currentFiles = openFiles.slice();
currentFiles.splice(index, 1);