如何在ReactJS中的另一页上的一个页面上渲染文本?

发布于 2025-02-13 16:23:55 字数 2155 浏览 0 评论 0原文

基本上,我只想显示另一个页面上文本框中输入的用户的名称。我是Reactjs的初学者,与我很裸露。

我正在在enter.js中的文本框中输入名称,然后单击按钮后,它将我重定向到preview.js,我希望在其中显示名称!!!

这是我的app.js代码

//import logo from './logo.svg';
import './App.css';
import React from 'react';
//import { useState } from 'react';
//import { useNavigate } from "react-router-dom";
//import Preview from './components/Preview';

import {
  //BrowserRouter as Router,
  Routes,
  Route
} from 'react-router-dom';
import Enter from './components/Enter';
function App() {

  //const [text,setText] = useState();

  
 
  return (
    <Routes>
      <Route path="/" element={<Enter/>}/>
      
    </Routes>
    
  );
}

export default App;

enter.js of enter.js

import React from 'react';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
//import PropTypes from 'prop-types'
import {
    //BrowserRouter as Router,
    Routes,
    Route
    //Link
  } from 'react-router-dom';
import Preview from './Preview';

export default function Enter() {
    const navigate = useNavigate();

  const preview = () => {
    navigate("/preview");
  }

  const [text,setText] = useState('');

  const handleChange = (event) => {
    setText(event.target.value);
  }
  return (
    <>
    <div className="container my-3">
    <label htmlFor="exampleFormControlInput1" className="form-label">Name:</label>
      <input className="form-control" placeholder="Enter your full Name" onChange={handleChange} value={text}/>
      <button className="btn btn-primary my-4" onClick={preview}>Submit</button>
    
    </div>
    <Routes>
    <Route path="/preview" element={<Preview text1={text}/>}/>
    </Routes>
    </>
  )
}


预览码


import React from 'react';
import PropTypes from 'prop-types';
export default function Preview(props) {
  return (
    <div className="container my-3">
        <h3>Name:{props.text1}</h3>
    </div>
  )
}

Preview.propTypes = {
    text1: PropTypes.string
}

Basically, I just want to display the name of the User entered inside the textbox on another page. I am a beginner in ReactJS so bare with me.

I am entering the name in a textbox in Enter.js and upon clicking the button it is redirecting me to Preview.js where I want the name to be displayed!!!

Here is my code of App.js

//import logo from './logo.svg';
import './App.css';
import React from 'react';
//import { useState } from 'react';
//import { useNavigate } from "react-router-dom";
//import Preview from './components/Preview';

import {
  //BrowserRouter as Router,
  Routes,
  Route
} from 'react-router-dom';
import Enter from './components/Enter';
function App() {

  //const [text,setText] = useState();

  
 
  return (
    <Routes>
      <Route path="/" element={<Enter/>}/>
      
    </Routes>
    
  );
}

export default App;

Code of Enter.js

import React from 'react';
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
//import PropTypes from 'prop-types'
import {
    //BrowserRouter as Router,
    Routes,
    Route
    //Link
  } from 'react-router-dom';
import Preview from './Preview';

export default function Enter() {
    const navigate = useNavigate();

  const preview = () => {
    navigate("/preview");
  }

  const [text,setText] = useState('');

  const handleChange = (event) => {
    setText(event.target.value);
  }
  return (
    <>
    <div className="container my-3">
    <label htmlFor="exampleFormControlInput1" className="form-label">Name:</label>
      <input className="form-control" placeholder="Enter your full Name" onChange={handleChange} value={text}/>
      <button className="btn btn-primary my-4" onClick={preview}>Submit</button>
    
    </div>
    <Routes>
    <Route path="/preview" element={<Preview text1={text}/>}/>
    </Routes>
    </>
  )
}


Code of Preview.js


import React from 'react';
import PropTypes from 'prop-types';
export default function Preview(props) {
  return (
    <div className="container my-3">
        <h3>Name:{props.text1}</h3>
    </div>
  )
}

Preview.propTypes = {
    text1: PropTypes.string
}

Thankyou for your help in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぇ气 2025-02-20 16:23:55

实现此目的的最佳方法是通过这样的重定向传递查询参数:

const preview =()=&gt; {
导航(/preview?q = $ {text});
}

然后在预览页面上,您可以获取查询参数的值并在页面上显示。

import {useearchParams}来自“ react-router-dom”,

让[searchParams,setSearchParams] = useechapparams()
const text = searchparams.get(“ q”)

这就是您可以获取参数的方式

The best way to achieve this is to pass query parameters on redirection like this:

const preview = () => {
navigate(/preview?q=${text});
}

And then on the preview page you can get the value of query param and display it on the page.

import { useSearchParams } from "react-router-dom"

let [searchParams, setSearchParams] = useSearchParams()
const text= searchParams.get("q")

This is how you can get the params

猫烠⑼条掵仅有一顆心 2025-02-20 16:23:55

最佳选择是使用 react上下文将文本保存在全球状态上,然后从您的预览组件或您想要的任何组件。

Best option is to use React Context to save your text on global state , then read that from your Preview Component, or any component you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文