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

343
Visualizações
React, pass state between sibling components

I am working on a simple ShoppinList. I have two components, Form.js and List.j. How can I display in List.js the items that are stored in list array in Form.js ? I don't want useState to be written in App.js, but would keep the logic separate in its own components.

App.js:

//Imporing Components
import Form from "./Components/Form";
import List from "./Components/List";

function App() {
  return (
      <div className="App">
          <header>
              <h1>Shopping List</h1>
          </header>
          <Form/>
          <List/>
      </div>
  )
}

Form.js:

/*Create a Component*/
const Form = () => {

    const [inputText,setInputText] = useState("")
    const [list,setList] = useState([])

    const submitBtn = (e) =>{
         e.preventDefault()  /*kein refresh tätigen*/
        setList([
            ...list,{inputText}
        ])
    }

    /*Funktion*/
    /*const inputTextHandler =  (e) => {
        setInputText(e.target.value)
    }*/

    return (
        <form>
            <input
                value={inputText}
                type="text"
                onChange={
                    (e) => setInputText(e.target.value)
                }
            />
            <button type="submit" onClick={submitBtn}>
                <i>Add</i>
            </button>
        </form>
    )
}
export default Form

List.js:

import React, {useState} from "react";

const List = () =>{
    return(
        <div>
            <ul>{[List]}</ul>
        </div>
    )
}
export default List
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In React state goes top to bottom. A nested component can update the state of a parent if a function defined in the parent has been passed to it as props. Which means, what you wanna do is not possible as you wanna pass state between sibling components (List.js and Form.js).

For this to work, you should have the state for the list in a parent component, App.js for example, this way:

import Form from "./Components/Form";
import List from "./Components/List";
import {useState} from "react";


function App() {
  const [list, setList] = useState([])
  return (

      <div className="App">
          <header>
              <h1>Shopping List</h1>
          </header>
          <Form list = {list} setList = {setList}/>
          <List list = {list} />
      </div>
  )
}
import React, {useState} from "react";

const List = ({list}) =>{

    return(
        <div>
            <ul>{list.map(item => <li>"test"</li>)}</ul>
        </div>
    )
}

export default List
const Form = ({list, setList}) => {
    const [inputText,setInputText] = useState("")

    const submitBtn = (e) =>{
         e.preventDefault()  
        setList([
            ...list,{inputText}
        ])
    }

    return (
        <form>
            <input
                value={inputText}
                type="text"
                onChange={
                    (e) => setInputText(e.target.value)
                }
            />
            <button type="submit" onClick={submitBtn}>
                <i>Add</i>
            </button>
        </form>
    )
}

export default Form
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't pass props to sibling component without passing it to parent component. Alternative way is to use state managers such as Redux, Effector or at least React Context.

The way React works:

import Form from "./Components/Form";
import List from "./Components/List";


function App() {
    const [list, setList] = useState([]);


    const formSubmitHandler = (passedData) => {
        setList(passedData);
    }

    return (
         <div className="App">
             <Form onSubmitting={formSubmitHandler}/>
             <List list={list}/>
         </div>
    )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't really do what you're asking as React only allows child components to accept state from parent components, it is a top-down process. I would recommend using a React "Context"; A context will allow you to have one component that can share state throughout the entire component tree without having to pass props down through child components. It's basically a store for the whole react app, that can be pulled wherever and whenever needed.

https://reactjs.org/docs/context.html

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