我要做的才能声明“ app.js”中的变量。在react.js中,并在“ return()”中使用此变量。进入应用功能?
当我单击一个橙色按钮时,将“ P1”变量打印到控制台,但该变量的值为null 我不知道为什么这个变量会给我这个错误,我想在按钮内的onclick函数中使用它。
从“ ./images/bg-pattern-desktop.svg”中导入img_1; 从“ ./images/illustration-woman-online-desktop.svg”导入img_2; 从“ ./images/illustration-box-desktop.svg”导入img_3;
函数应用程序(){ const p1 = document.queryselector(“ p1”);
返回( <> < img src = {img_3} alt =“ img_3” className =“ img_3” /> < div className =“容器”> < div className =“ imgbox”> < img src = {img_1} alt =“ img_1” className =“ img_1” /> < img src = {img_2} alt =“ img_2” className =“ img_2” /> </div> < div className =“ textbox”> < h1> faq</h1> < div className =“ wordity”> < div> {/ * p1的值为null */} {/ *因为它无法获得document.queryselector(“。p1”) */} <按钮 onClick = {()=> { console.log(p1); }}} > 我可以邀请多少个团队成员? </butt> < p className =“ p1”> lorem ipsum dolor sit amet consectur,掺杂的Elit。 Quaibus obcaecati,Quae,Corporis。 </p> </div> < hr /> < div> < button onClick = {()=> {}}> 我可以邀请多少个团队成员? </butt> < p className =“ notactive p2”> lorem ipsum dolor sit amet consectur,掺杂的Elit。 Quaribus obcaecati,Quae,Corporis。 </p> </div> < hr /> < div> < button onClick = {()=> {}}> 我可以邀请多少个团队成员? </butt> < p className =“ notactive p3”> lorem ipsum dolor sit amet consectur,掺杂的Elit。 Quaibus obcaecati,Quae,Corporis。 </p> </div> < hr /> < div> < button onClick = {()=> {}}> 我可以邀请多少个团队成员? </butt> < p className =“ notactive p4”> lorem ipsum dolor sit amet consectur,掺杂的Elit。 Quaribus obcaecati,Quae,Corporis。 </p> </div> < hr /> < div> < button onClick = {()=> {}}> 我可以邀请多少个团队成员? </butt> < p className =“ notactive p5”> lorem ipsum dolor sit amet consectur,掺杂的Elit。 Quaibus obcaecati,Quae,Corporis。 </p> </div> < hr /> </div> </div> </div> </> );
}
导出默认应用程序;
the image of the final project
when I click on one of the orange buttons it should print the "p1" variable to the console but the value of this variable is Null
I don't know why this variable gives me that error and I want to use it in the onClick function inside the buttons.import img_1 from "./images/bg-pattern-desktop.svg";
import img_2 from "./images/illustration-woman-online-desktop.svg";
import img_3 from "./images/illustration-box-desktop.svg";function App() {
const p1 = document.querySelector("p1");return ( <> <img src={img_3} alt="img_3" className="img_3" /> <div className="container"> <div className="imgBox"> <img src={img_1} alt="img_1" className="img_1" /> <img src={img_2} alt="img_2" className="img_2" /> </div> <div className="textBox"> <h1>FAQ</h1> <div className="questions"> <div> {/* the value of p1 is null */} {/* because it can't get document.querySelector(".p1") above */} <button onClick={() => { console.log(p1); }} > How many team members can I invite? </button> <p className="p1"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Temporibus obcaecati, quae, corporis. </p> </div> <hr /> <div> <button onClick={() => {}}> How many team members can I invite? </button> <p className="notactive p2"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Temporibus obcaecati, quae, corporis. </p> </div> <hr /> <div> <button onClick={() => {}}> How many team members can I invite? </button> <p className="notactive p3"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Temporibus obcaecati, quae, corporis. </p> </div> <hr /> <div> <button onClick={() => {}}> How many team members can I invite? </button> <p className="notactive p4"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Temporibus obcaecati, quae, corporis. </p> </div> <hr /> <div> <button onClick={() => {}}> How many team members can I invite? </button> <p className="notactive p5"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Temporibus obcaecati, quae, corporis. </p> </div> <hr /> </div> </div> </div> </> );
}
export default App;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里没有错误,只是我认为您认为您的误解。您的问题是尝试在React组件中使用
document.queryselector
。简单答案:
如下所示,请使用
useref
。另外,请查看 docs 以更好地理解。完整答案:
当组件首先渲染时,
document.queryselector
在使用类“ p1”元素返回元素之前运行。因此,它尚未在页面上重新重新进行过重新进行。那里的document.queryselector
将无法找到该元素,而是返回null。There is no error here, simply a misunderstanding on your behalf I presume. Your issue is trying to use
document.querySelector
within the react component.Simple Answer:
Use
useRef
instead as below. Also, checkout the docs for a better understanding.Full Answer:
When the compoent first renders,
document.querySelector
is run before you return the element with the class "p1". Therefore it hasn't yet been redered on the page. Theredocument.querySelector
will fail to find the element, returning null instead.用
QuerySelector
字符串来定位类需要一个点:如果要将变量存储在React组件中,我建议您查看 usestate()而不是查询选择器:
Targeting a class with a
querySelector
string needs a dot:If you want to store a variable in a React component, I suggest you look into useState() instead of a query selector: