简体   繁体   English

React:如何从父组件重新加载数据并在子组件更新时呈现?

[英]React: How to reload data from parent component and render when child component do update?

I've 1 Parent component, 1 child component, and 1 child's child component.我有 1 个父组件、1 个子组件和 1 个子组件。 Im using Jotai state management.我正在使用 Jotai state 管理。

Parent Component:父组件:

  • Have a button to view LogDetails.js (Dialog)有个按钮可以查看LogDetails.js(Dialog)
   const viewLog = useSetAtom(setViewStatusLogDialog);


      const handleShowStatusLog = (data) => {
             viewLog({
               open: true,
               data,  // the data I used in child component 
             });   };

Child component (LogDetails.js):子组件(LogDetails.js):

  • To view all log details查看所有日志详细信息
  • Have a create button to add new log(Will pop up another Dialog)有一个创建按钮来添加新日志(会弹出另一个对话框)
    export const setViewStatusLogDialog = atom(null, (_get, set, update)
    => {   const { open, data} = update;   
   set(openStatusLogDialogAtom, open);   
   set(dataAtom, data);   });

Child's child component (create form)孩子的子组件(创建表单)

  • Here to do submit and save.这里要做提交和保存。

My Problem is:我的问题是:

When I do save in the create form (Child's child component), the log details (child component) wont get the latest data and show it, how can I do that?当我在创建表单(子组件)中保存时,日志详细信息(子组件)不会获取最新数据并显示它,我该怎么做?

Not sure about Jotai state management, but you can simple pass a function from Child Component (LogDetails.js) to Child's child component and call that function with new data on save.不确定 Jotai state 管理,但您可以简单地将 function 从子组件 (LogDetails.js) 传递到子组件的子组件,并在保存时使用新数据调用该 function。

ChildComponent子组件

function ChildComponent() {
  const viewLog = useSetAtom(setViewStatusLogDialog);
  const setNewLogData = newData => {
    const data = [...oldData, newData];
    viewLog({open: true, data });
  }

  return <>
    {logData.map(data => <p>{data}</p>}
    <ChildChildComp setNewLogDataToParent={setNewLogData} />
  </>
}

Child's child component Child 的子组件

function ChildComponent(props) {
  const [data, setData] = useState();
  const onSave = () => {
     ...
     props.setNewLogDataToParent(data);
  }
  return <>
    <input type="text" onChange={e => setData(e.target.value) />
    <button onClick={onSave}>Save</button>
  </>
}

暂无
暂无

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

相关问题 如何在 React 中渲染和更新来自父组件的子元素? - How to render and update child elements coming from a parent component in React? 当父组件中发生事件时,如何从子组件更新父组件中的 state - How do you update state in parent component from a child component when an event takes place in the parent component 如何使用从React中的子级获取的数据更新父级组件中的状态 - How to update state in parent component with data taken from child in React React中子组件更新时如何更新父组件 - How to Update the parent component when child component updates in React 如何从 React 中的子组件更新父组件(功能) - How to update Parent component from Child component in React (Functional) 在React.js的父组件中使用react-router时,如何使用react context API将数据从父组件传递到子组件? - How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js? 更改父状态时如何防止子组件重新加载 - how to prevent child component from reload when the parent state is change 从父组件 React JS 调用 map function 时如何更新子组件的 state - How to update state of child component when it get called in map function from parent component React JS 如何在反应中从子组件获取数据到父组件? - how to get data from child component to parent component in react? 如何将数据从反应子组件传输到父组件 - How to transfer data from react child component to parent component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM