不同文件组件的 state 和 props
如果您的问题的答案很明显,我提前表示歉意,但我是 React 新手,无法在您的网站上找到答案。 我在同一文件夹的不同文件中有两个组件。我想获取组件 2 中组件 1 的状态,但它给了我一个错误。如何获取不同文件中组件的状态或属性? 我如何知道哪个元素生成事件,例如,如果我有 7 个按钮,并且我想知道其中哪个元素生成事件,也来自不同文件的组件。 这些组件位于同一文件夹中: 地毯a1← 组件1.js componente2.js
代码如下:
//componente1.js
import React from 'react'
import Link from 'next/link'
import Image from 'next/image'
import cn1 from '../../public/lared.png'
class Componente1 extends React.Component{
constructor(props){
super(props);
this.cambiarSenal = this.cambiarSenal.bind(this);
this.state = {senal: ""};
}
cambiarSenal(event){
let btn = event.target;
this.setState({
senal: 'https://www.lared.cl/senal-online'
});
}
render(){
return(
<div className="root">
<div className={styles.ctElm}>
<div className={styles.ctImg} id="ctImg"><Image src= {cn1} alt='cn1' className={styles.img}/></div>
<p className={styles.ctTt} onClick={this.cambiarSenal}><Link href='/carpeta1/senalLive'><a className={styles.enl}>La Red</a></Link></p>
</div>
</div>
</div>
});
}
export default Componente1;
//componente2.js
import React from 'react'
class SenalLive extends React.Component{
render(){
return(
<div className={styles.root}>
<div className={styles.cnl}>
<iframe className={styles.frm} src={state.senal}></iframe>
</div>
</div>
});
}
export return SenalLive;
总结一下。在 iframe 中,我将组件的状态称为 1。 我使用 state 是因为我要使用几个
,每个都会有更改使用 component2 的链接的事件,这将根据状态的变化更改 iframe 的 url
I apologize in advance if the answer to your question is obvious but I'm new to React and can't find the answer on your website.
I have two components in different files in the same folder. I want to get the state of component1 in component2 but it gives me an error. How can I get the state or props of components that are in different files?.
How can I know which element generates an event, for example if I have 7 buttons and I want to know which of them generates an event, also from components of different files.
The components are in the same folder:
carpeta1¬
componente1.js
componente2.js
The code is the following:
//componente1.js
import React from 'react'
import Link from 'next/link'
import Image from 'next/image'
import cn1 from '../../public/lared.png'
class Componente1 extends React.Component{
constructor(props){
super(props);
this.cambiarSenal = this.cambiarSenal.bind(this);
this.state = {senal: ""};
}
cambiarSenal(event){
let btn = event.target;
this.setState({
senal: 'https://www.lared.cl/senal-online'
});
}
render(){
return(
<div className="root">
<div className={styles.ctElm}>
<div className={styles.ctImg} id="ctImg"><Image src= {cn1} alt='cn1' className={styles.img}/></div>
<p className={styles.ctTt} onClick={this.cambiarSenal}><Link href='/carpeta1/senalLive'><a className={styles.enl}>La Red</a></Link></p>
</div>
</div>
</div>
});
}
export default Componente1;
//componente2.js
import React from 'react'
class SenalLive extends React.Component{
render(){
return(
<div className={styles.root}>
<div className={styles.cnl}>
<iframe className={styles.frm} src={state.senal}></iframe>
</div>
</div>
});
}
export return SenalLive;
It is summarized. In the iframe I would call the state of the component1.
I am using state because I am going to use several
, each one will have the event of changing the link for the use of component2 which will change the url of the iframe according to the change of state
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论