簡體   English   中英

當父組件改變狀態時,子組件不會獲得新的道具(功能組件)

[英]Child component not get new props when parent component change state ( Functional Component )

為什么當父組件改變狀態時我的子組件沒有獲得新的道具。 我使用數據庫中包含 3 個元素的列表來測試和聲明 Add_Friend_Modal 中的 list_user_selected 以存儲所有子 Add_Friend_Selected。 我嘗試當用戶單擊 Add_Friend_Modal 中的刪除按鈕時,狀態 list_user_selected 將被更新。 但是當我單擊第一個子組件的按鈕刪除時,props.list_user_selected 剛剛得到數組 []。 第二個子組件獲得數組 [第一個子組件]。 第三個子組件得到數組[第一個子組件,第二個子組件]。 我已經嘗試了一切,但沒有奏效。 誰能向我解釋為什么,以及如何解決它

子組件

const Add_Friend_Selected = props => {
    return (
        <li className="list-group-item px-0 d-flex">
            <figure className="avatar mr-3">
                <img src={props.avatar} className="rounded-circle" alt="image" />
            </figure>
            <div>
                <div>{props.name}</div>
                <div className="small text-muted">{props.mobile}</div>
            </div>
            <a onClick={() => props.delete_user_selected(props.id)} className="text-danger ml-auto" data-toggle="tooltip" title="Remove">
                <i className="mdi mdi-delete-outline"></i>
            </a>
        </li>
    )
}

父組件

const Add_Friend_Modal = props => {
    const [count_user_selected,set_count_user_selected] = useState(0);
    const [list_user_selected,set_list_user_selected] = useState([]);
    const user = useSelector((state) => state.user);
    const input_mobile_friend = useRef("");
    
    const delete_user_selected = (id) => {
        const delete_user_index = list_user_selected.findIndex(user_selected => user_selected.props.id === id);
        console.log(delete_user_index)
        set_list_user_selected([...list_user_selected.splice(delete_user_index,1)])
    }
    const add_invite_friend = () => {
        if(input_mobile_friend.current.value) {
            call_api({
                url : `/users/search?mobile=${input_mobile_friend.current.value}`
            })
                .then(response => {
                    const user_find = response.data.data;
                    if(user_find && !list_user_selected.some(user_selected => user_selected.props.id === user_find._id )) {
                        const new_user_selected = (
                            <Add_Friend_Selected name={user_find.name} avatar={user_find.avatar} 
                                                 mobile={user_find.mobile} id={user_find._id} 
                                                 list_user_selected={list_user_selected}
                                                 delete_user_selected={(id) => {
                                                     delete_user_selected(id)
                                                     set_count_user_selected(list_user_selected.length + 1)
                                                 }}
                                                 />
                        )

                        set_list_user_selected([...list_user_selected,new_user_selected])
                        set_count_user_selected(list_user_selected.length + 1)
                    }
                    else {
                        console.log("Not found")
                    }
                })
                .catch(error => {
                    console.log(error)
                })
        }
    }

修復代碼的步驟:

  • 在'useEffect'中的鈎子的情況下,不要在'componentDidMount'以外的地方調用apis

  • 如果你想使用一個函數來渲染你的組件,你需要在你的功能組件的返回方法中調用它

看看這段代碼是否可以幫助你:

//state for update when your api call finished
const [user_find, set_user_find] = useState(null)

//useEffect for call your api
useEffect(() => {
  //componentSkillMount its a variable for not update an unmounted component
  let componentSkillMount = true;
  call_api({
    url : `/users/search?mobile=${input_mobile_friend.current.value}`
    })
    .then(response => {
      //if your component skill mounted you can update that
      componentSkillMount && set_user_find(response.data.data)
    })
  return () => componentSkillMount = false
}, [])

return (
  //if the api call finished you can return a full component
  user_find && (
    <Add_Friend_Selected name={user_find.name} avatar={user_find.avatar} 
      mobile={user_find.mobile} id={user_find._id} 
      list_user_selected={list_user_selected}
      delete_user_selected={(id) => {
          delete_user_selected(id)
          set_count_user_selected(list_user_selected.length + 1)
      }}
      />
  )
)

嘗試將此代碼調整為您的:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM