我需要将功能组件转换为基于类的组件。我对 React 相当陌生,但我尝试过转换该组件。我收到错误 - ReferenceError:找不到变量:Props
我的问题是,在哪里添加基于类的组件中存在的 Prop 才能使转换工作?
基于类的组件是一个模态,具有从父组件触发的表单,效果很好。该表单使用的状态变量在基于类的组件中不起作用,因此我需要将当前的功能组件转换为基于类的组件。我使用的是 React 16.6.3 版本,因为其他包不适用于较新版本的 React-Native,因此我无法在该版本中使用钩子(Hook)。
功能组件
const ShowModal = props => (
<Modal
visible={props.display}
animationType="slide"
onRequestClose={() => console.log("closed")}
>
...Other stuff in here.
</Modal>
);
export default ShowModal;
基于类的组件
export default class ShowModal extends Component {
state = {
};
render() {
return (
...Other stuff in here
);
}
}
我收到错误 - ReferenceError:找不到变量:Props
最佳答案
在基于类的组件中,props
在类的主作用域中公开。您应该使用 this
关键字来阅读它
class Component extends React.Component{
render(){return this.props.value}
}
关于javascript - 将功能组件转换为基于类的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57691910/