我必须使用从 a.tsx 到另一个 b.tsx 文件的 Abc 变量。如何在另一个 .tsx 文件中访问该 Abc 变量?
当前 a.tsx:
export const ProfilePicEditor = (props: ProfilePicEditorProps) => {
const [Abc, setAbc] = useState(
'some string'
)
我尝试在我的 a.tsx 文件中执行“export {Abc}”并将其作为“Import {Abc} from 'a.tsx”导入到 b.tsx 文件中。但它没有成功。有什么可能的原因吗?
您应该将变量作为道具传递给子组件,如果父组件想要加入该变量,您可以使用 ref
class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.ChildComponent = React.createRef();
}
render() {
return <ChildComponent ref={this.ChildComponent} />;
}
}
你可以这样使用它:
const currentChildComponent = this.currentChildComponent.current;
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句