gpt4 book ai didi

javascript - react : How to setState in child component from parent component?

转载 作者:行者123 更新时间:2023-12-05 04:35:54 26 4
gpt4 key购买 nike

我是 React 的新手,正在尝试设置一个 Bootstrap 模式来显示警报消息。

在我的父 App.js 文件中,我有一个错误处理程序,它向 Modal.js 组件发送一个触发模态显示的 Prop ,例如:

在 App.js 上:

function App() {

const [modalShow, setModalShow] = useState(false);

// Some other handlers

const alertModalHandler = (modalMessage) => {
console.log(modalMessage);
setModalShow(true);
}

return (
// Other components.
<AlertModal modalOpen={modalShow}/>
)
}

在 Modal.js 上:

import React, { useState } from "react";
import Modal from "react-bootstrap/Modal";
import "bootstrap/dist/css/bootstrap.min.css";

const AlertModal = (props) => {

const [isOpen, setIsOpen] = useState(false);

if (props.modalOpen) {
setIsOpen(true);
}

return (
<Modal show={isOpen}>
<Modal.Header closeButton>Hi</Modal.Header>
<Modal.Body>asdfasdf</Modal.Body>
</Modal>
);
};

export default AlertModal;

但是,这是行不通的。我收到错误:

Uncaught Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.

如果我将 Modal 组件更改为“哑”组件并直接使用 prop,例如:

 const AlertModal = (props) => {

return (
<Modal show={props.modalOpen}>
<Modal.Header closeButton>Hi</Modal.Header>
<Modal.Body>asdfasdf</Modal.Body>
</Modal>
);
};

它确实有效,但我也想更改 Modal.js 组件级别的显示/隐藏状态,例如,有一些东西可以处理那里的模态关闭按钮。

我不明白为什么会这样?

这是否意味着我必须在父级 App.js 级别处理模态关闭功能?

编辑 - 完整的 app.js 内容

import React, { useState } from 'react';

import './App.css';
import 'bootstrap/dist/css/bootstrap.css';
import AddUserForm from './components/addUserForm';
import UserList from './components/userList';
import AlertModal from './components/modal';

function App() {

const [users, setUsers] = useState([]);
const [modalShow, setModalShow] = useState(false);


const addPersonHandler = (nameValue, ageValue) => {
console.log(nameValue, ageValue);

setUsers(prevUsers => {
const updatedUsers = [...prevUsers];
updatedUsers.unshift({ name: nameValue, age: ageValue });
return updatedUsers;
});
};

const alertModalHandler = (modalMessage) => {
console.log(modalMessage);
setModalShow(true);
}

let content = (
<p style={{ textAlign: 'center' }}>No users found. Maybe add one?</p>
);

if (users.length > 0) {
content = (
<UserList items={users} />
);
}

return (
<>
<div className="container">
<div className="row">
<div className="col-md-6 offset-md-3">
<AddUserForm onAddPerson={addPersonHandler} fireAlertModal={alertModalHandler}/>
</div>
</div>
<div className="row">
<div className="col-md-6 offset-md-3">
{content}
</div>
</div>
</div>
<AlertModal modalOpen={modalShow}/>
</>
);
}

export default App;

最佳答案

在你的 modal.js 中

你应该把

if (props.modalOpen) {
setIsOpen(true);
}

在 useEffect 中。

React.useEffect(() => {if (props.modalOpen) {
setIsOpen(true);
}}, [props.modalOpen])

关于javascript - react : How to setState in child component from parent component?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70936509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com