this is AddRoute.jsx import { useState, useEffect } from 'react'; import { Container,…
- User
this is AddRoute.jsx import { useState, useEffect } from 'react'; import { Container, Row, Col } from 'react-bootstrap'; import { BackButton } from './NavigationComponents'; import { BlockList, CreatePage, ActionList, Title } from './AddComponents'; import ErrorRoute from './ErrorRoute';
function AddRoute(props) {
const [list, setList] = useState([]); // Used as array of objects const [title, setTitle] = useState(""); const [date, setDate] = useState(""); // Used to publish programmed pages //const [dirtylist, dirtySetList] = useState(true); return ( props.loggedIn ? ( // If not loggedin view ErrorRoute <Container fluid> <p></p> <Row> <Col xs={2}> <BackButton /> </Col> <Col> <h2 style={{textAlign: "center"}}>Create Page</h2> <p></p> <Title title={title} setTitle={setTitle} /> <p></p> <CreatePage list={list} setList={setList} /> </Col> <Col xs={2}> <Col style={{ position: "relative", paddingRight: "10%", top: "50px" }}> <BlockList list={list} setList={setList} /> <ActionList date={date} setDate={setDate} setList={setList} title={title} list={list} handleError={props.handleError} /> </Col> </Col> </Row> </Container> ) : <ErrorRoute /> );}
export default AddRoute;
this is AddComponents.jsx import 'bootstrap-icons/font/bootstrap-icons.css'; import { useState, useEffect } from 'react'; import { Col, ListGroup, Button, Form, FormGroup, Accordion, Card, ButtonGroup } from 'react-bootstrap'; import { useNavigate } from 'react-router-dom'; import API from '../API'; import dayjs from 'dayjs';
function BlockList(props) {
function headerBlock() { this.blockType = "Header"; this.content = ""; } function paragraphBlock() { this.blockType = "Paragraph"; this.content = ""; } function imageBlock() { this.blockType = "Image"; this.content = ""; } const addToList = (x) => { props.setList((oldList) => [...oldList, x]); }; return ( <> <h5 style={{ textAlign: "center" }}>Components</h5> <p></p> <Card> <ListGroup> <ListGroup.Item >Header<Button className="float-end" variant="outline-success" style={{ fontSize: '21px', display: "flex", justifyContent: 'center', alignItems: 'center', width: '25px', height: '25px', borderRadius: '40px' }} onClick={() => { addToList(new headerBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item >Paragraph<Button className="float-end" variant="outline-success" style={{ fontSize: '21px', display: "flex", justifyContent: 'center', alignItems: 'center', width: '25px', height: '25px', borderRadius: '40px' }} onClick={() => { addToList(new paragraphBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item >Image<Button className="float-end " variant="outline-success" style={{ fontSize: '21px', display: "flex", justifyContent: 'center', alignItems: 'center', width: '25px', height: '25px', borderRadius: '40px' }} onClick={() => { addToList(new imageBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> </ListGroup> </Card> </> ); - Assistant
The error is caused by the use of
setIsAligned(!isAligned)inside functions likeremoveFromList,handleUp, andhandleDown. These functions are called within the render, which causes an updated state in the componentMyBlock, while theAddRoutecomponent is being rendered.Instead of triggering a re-render using
setIsAligned, you can add a key to theMyBlockcomponent to make it update when the list changes. To do this, you should remove thesetIsAlignedcall inremoveFromList,handleUp, andhandleDownfunctions. Then, modify the key of theMyBlockcomponent in theCreatePagefunction:Replace this:
{props.list.map((x, index) => <MyBlock x={x} key={index} index={index} list={props.list} setList={props.setList} />)}With this:
{props.list.map((x, index) => <MyBlock x={x} key={`${x.blockType}-${index}`} index={index} list={props.list} setList={props.setList} />)} - User
key={{index}} does not trigger re-render or is a syntax error
- Assistant
Apologies for the syntax oversight. The correct syntax should be wrapped inside curly braces
{}and use backticks`for the template string. Please update the line like this:{props.list.map((x, index) => <MyBlock x={x} key={`${x.blockType}-${index}`} index={index} list={props.list} setList={props.setList} />)}If this doesn't solve the issue, try generating a unique key for each element in the list when you create the blocks. For example:
function headerBlock() { this.key = "header-" + Math.random().toString(36).substr(2, 9); this.blockType = "Header"; this.content = ""; } function paragraphBlock() { this.key = "paragraph-" + Math.random().toString(36).substr(2, 9); this.blockType = "Paragraph"; this.content = ""; } function imageBlock() { this.key = "image-" + Math.random().toString(36).substr(2, 9); this.blockType = "Image"; this.content = ""; }And then use these unique keys in your
MyBlockcomponent like this:{props.list.map((x, index) => <MyBlock x={x} key={x.key} index={index} list={props.list} setList={props.setList} />)}
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.