简体   繁体   English

将更新的父状态传递给子道具 React.js

[英]Pass updated parent state to child props React.js

I have two child components in my parent component with routing set up.我的父组件中有两个子组件,并设置了路由。 Child1 component's button updates state of the parent component and I want to pass the updated version of that state to child2 component as props. Child1 组件的按钮更新父组件的状态,我想将该状态的更新版本作为道具传递给 child2 组件。 The problem is, although the state obviously updates in the parent component(I checked it), it doesn't get passed to the child2 component(the older version of state gets passed instead).问题是,虽然状态在父组件中明显更新(我检查了它),但它没有传递给 child2 组件(而是传递了旧版本的状态)。 I think it's because the getdData function(in the parent component) executes after the child2 component gets rendered.我认为这是因为getdData函数(在父组件中)在getdData组件被渲染后执行。 How can I solve this issue so that correct state gets passed?我该如何解决这个问题,以便通过正确的状态?

Here's the code:这是代码:

Parent component:父组件:

import './App.css';
import Questions from './components/questions';
import DropDownDifficulty from './components/dropDownDifficulty';
import {useState} from 'react'
import axios from 'axios';
import {BrowserRouter as Router, Route, Redirect} from 'react-router-dom'

function App() {
  
  const [data, setdata] = useState([])
  const getdData = (values)=>{
    setdata(values)
  }
  return (
    <div className="App">
      <Router>
        <Route exact path='/'>
          <DropDownDifficulty sendData={getdData}/>
        </Route>
        <Route exact path = '/quiz'>
          <Questions specs={data}/>
        </Route>
      
      </Router>
    </div>
  );
}

export default App;

Child1 component's button: Child1 组件的按钮:

<Button  disabled={buttonDisabled} className='button' variant="contained" color="primary" onClick={()=>sendData([category, Difficulty])}>
            Start Quiz
        </Button>

child2 component: child2 组件:

import React from 'react'
import Question from './question'


export default function Questions({specs}) {
    console.log(specs)
    return (
        <div>
        </div>
    )
}

Simple Solution :简单的解决方案:

1.Check for Correct value before passing it down to props 1.在将其传递给道具之前检查正确的值

<div className="App">
      <Router>
        <Route exact path='/'>
          <DropDownDifficulty sendData={getdData}/>
        </Route>
        {data?.length > 0 && <Route exact path = '/quiz'>
          <Questions specs={data}/>
        </Route>}
      </Router>
    </div>
  1. You can use useEffect and listen to props and show value only after the correct data comes只有在正确的数据到来后才能使用useEffect并监听props和show value

import {useEffect} from 'react'
import Question from './question'


export default function Questions({specs}) {
    const [showIfCorrect,setShowIfCorrect]=useState(false)
    useEffect(()=>{
        if(specs){
          // Check if it is correct
          setShowIfCorrect(true)
         }
    },[specs])
    return (
        <div>
          {showIfCorrect && <div>Correct Question</div>}
        </div>
    )
}
  1. use Context Api and update state from sibling component(DropDownDifficulty) and pass it to the Questions component使用 Context Api 并从兄弟组件(DropDownDifficulty)更新状态并将其传递给 Questions 组件

For How to implement in context api refer the code below对于如何在上下文 api 中实现,请参阅下面的代码编辑神经翼6f3nd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM