reactjs - 使用 vs 不使用构造函数设置上下文状态
问题描述
当我尝试在没有构造函数的情况下初始化我的状态时,我遇到了函数未绑定到状态的问题。尽管这样的教程在没有构造函数的情况下直接初始化状态,但执行以下操作不起作用。
这不会将功能传递给消费者:
class NewTownFormContextProvider extends Component {
state = {
towns:[],
name: '',
parish: '',
categories:[],
catList:[],
resetForm: this.resetForm,
updateTown: this.updateTown,
updateParish: this.updateParish,
setCategory: this.setCategory,
submit: this.submit
}
resetForm = ()=>{
this.setState({
name:'',
parish:'',
categories:[]
})
}
updateTown = (e) =>{
this.setState({town: e.target.value});
console.log(this.state.town);
}
updateParish = (e) =>{
this.setState({parish: e.target.value});
console.log(this.state.parish);
}
setCategory = (e) =>{
this.setState({category: e.target.value});
console.log(this.state.category);
}
submit = async (e) => {
e. preventDefault();
this.setState([...this.state.towns, {name: this.state.town, parish: this.state.parish}])
}
}
async componentDidMount(){
let data = await fetch(`${d}/town/get-categories`);
let catList = await data.json();
this.setState({ catList });
}
render(){
return(
<NewTownFormContext.Provider value={{...this.state }}>
{this.props.children}
</NewTownFormContext.Provider>
);
};
}
但这确实:
class NewTownFormContextProvider extends Component {
constructor(props){
super(props);
this.state = {
towns:[],
name: '',
parish: '',
categories:[],
catList:[],
resetForm: this.resetForm,
updateTown: this.updateTown,
updateParish: this.updateParish,
setCategory: this.setCategory,
submit: this.submit
}
}
resetForm = ()=>{
this.setState({
name:'',
parish:'',
categories:[]
})
}
updateTown = (e) =>{
this.setState({town: e.target.value});
console.log(this.state.town);
}
updateParish = (e) =>{
this.setState({parish: e.target.value});
console.log(this.state.parish);
}
setCategory = (e) =>{
this.setState({category: e.target.value});
console.log(this.state.category);
}
submit = async (e) => {
e. preventDefault();
this.setState([...this.state.towns, {name: this.state.town, parish: this.state.parish}])
}
}
async componentDidMount(){
let data = await fetch(`${d}/town/get-categories`);
let catList = await data.json();
this.setState({ catList });
}
render(){
return(
<NewTownFormContext.Provider value={{...this.state }}>
{this.props.children}
</NewTownFormContext.Provider>
);
};
}
我不确定为什么教程指定第一种方法。
解决方案
您需要在状态之前定义函数:
class App extends React.Component {
resetForm = () => {
console.log('reset form');
};
state = {
resetForm: this.resetForm,
};
render() {
this.state.resetForm();
return 'hi';
}
}
ReactDOM.render(<App />, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="root"></div>
这看起来像一个 xy 问题,不知道为什么需要将类方法添加到 state。
推荐阅读
- typescript - 如何使用 Typescript 用 Jest 模拟 tls.PeerCertificate?
- numpy - 过滤后的噪声信号 fft
- vim - 如何将可视范围传递给没有数字或在 vim 中使用 :command 的函数?
- google-api - 创建多个文件夹后,Google Drive Api (.Net) 返回错误
- python - 使用 python 的 Google Place API Json 数据
- javascript - 如何使用 supertest 和 chai 捕获 deferred.reject?
- django - 类型的对象不是 JSON 可序列化的
- instana - 如何在动态代理中设置模式和时间?
- java - 在 JDA 中担任角色
- sql - TSQL 在 IN 运算符中获取丢失的记录