Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

207
Visualizações
React.js using form input in another component

So I'm learning about how to use form input across my React app projects. I'm not really sure how to do it, props dosen't really seem to cut it. So if I have two components, one with a form, and another that I just want to show the input. I dont want to render the two components at once but rather wish to redirect the user to another view where the input is shown. Below is the code for the simple form.

import React from 'react'
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import Second from '../view/second/Second';

    const Form = () => {
    const navigate = useNavigate();

    const [namn, setNamn] = useState('');
    const [personNr, setPersonNr] = useState('');
    const [datum, setDatum] = useState('');
    const [planbok, setPlanbok] = useState('');

    
    return (

        <div>
            <form>
                <div>
                <input type="text" id="namnField" required value={namn} 
    onChange={(n) => setNamn(n.target.value)} placeholder='Namn'></input>
                </div>
                <div>
                <input type="text" id="personNrField" required value={personNr} 
    onChange={(pn) => setPersonNr(pn.target.value)} placeholder='Personnr'></input>
                </div>
                <div>
                <input type="text" id="datumField" required value={datum} 
    onChange={(d) => setDatum(d.target.value)} placeholder='Datum'></input>
                </div>
                <div>
                <input type="text" id="planbokField" required value={planbok} 
    onChange={(p)=>setPlanbok(p.target.value)} placeholder='Plånbokadress'></input>
                </div>
                <button type="submit" id="inputForm"onSubmit {navigate('/second')}>Enter</button>
            </form>
        </div>
    )
}

export default Form

And here is the component that I want to show the form input:

import React from 'react'
import Form from '../../form/Form'

const Second = ({namn, datum, personNr, planbok}) => {
    return (
        <div>
            <p>{namn}, {datum}, {personNr}, {planbok}</p>
        </div>
    )
}

export default Second

As you see I've tried to use props, this dosen't work. Sure I can render the component called in the component and I can populate with the form input. But this is not what I wish to achive, I want the variables from the form input to be shown when I render in a different view.

How would I achive this, should I use Ajax, the hook useContext? Is there some other way that is better to handle this type of situation? Thanks in advance for your time. Regards / Svante.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Solved this problem with useContext. Used the principles in this youtube video on the subject: https://www.youtube.com/watch?v=lhMKvyLRWo0&t=342s

I think it might be a better practice to do as MrPickles wrote as an answer to my first post.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda