import 'bootstrap-icons/font/bootstrap-icons.css'; import '../style.css'; import {…
- User
import 'bootstrap-icons/font/bootstrap-icons.css'; import '../style.css';
import { Accordion, Button, ButtonGroup, Card, Col, Form, ListGroup } from 'react-bootstrap'; import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import API from '../API'; import dayjs from 'dayjs';
//Can't get it from database because it is not saved. //This number is used only for triggering the re-render of the content of the Forms of each block showed at video. (ex when handleUp or handleDown) function generatelocalId() { return Math.random().toString(36).substring(2) + Date.now().toString(36); }
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 addbutton" variant="outline-success" onClick={() => { addToList(new headerBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item > Paragraph <Button className="float-end addbutton" variant="outline-success" onClick={() => { addToList(new paragraphBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> <ListGroup.Item > Image <Button className="float-end addbutton" variant="outline-success" onClick={() => { addToList(new imageBlock()); }}> <i className="bi bi-plus"></i> </Button> </ListGroup.Item> </ListGroup> </Card> </> );}
function CreatePageBody(props) {
return ( <Accordion> {props.list.map((x) => <MyBlock x={x} key={generatelocalId()} list={props.list} setList={props.setList} images={props.images} />)} </Accordion> );}
function MyBlock(props) {
const x = props.x; const images = props.images; const [content, setContent] = useState(x.content); const [notAligned, setNotAligned] = useState(false); const removeFromList = (x) => { props.setList((oldlist) => oldlist.filter((item) => item != x)); }; const handleUp = (x) => { props.setList(oldList => { const index = oldList.indexOf(x); const newList = [...oldList]; [newList[index - 1], newList[index]] = [newList[index], newList[index - 1]]; return newList; }); }; const handleDown = (x) => { props.setList(oldList => { const index = oldList.indexOf(x); const newList = [...oldList]; [newList[index], newList[index + 1]] = [newList[index + 1], newList[index]]; return newList; }); }; const handleContentChange = (x) => { props.setList((oldList) => { const newList = oldList.map((item) => (item === x ? { ...item, content: content } : item)); return newList; }) }; useEffect(() => { if(notAligned){ handleContentChange(x); setNotAligned(false); } }, [notAligned]); return ( <Accordion.Item eventKey={props.list.indexOf(x)}> <Accordion.Header > <div className="accordion-style"> {x.blockType} <ButtonGroup style={{ paddingRight: "2%" }} > {props.list.indexOf(x) != 0 && ( //button as div avoid nested buttons error validateDOMnesting - since accordion header is a button itself! <Button as='div' style={{ fontSize: "0.8rem", padding: "0.25rem 0.5rem" }} variant="outline-secondary" onClick={(ev) => { ev.stopPropagation(); handleUp(x) }}> <i className="bi bi-arrow-up"></i> </Button>)} {props.list.indexOf(x) != (props.list.length - 1) && ( <Button as='div' style={{ fontSize: "0.8rem", padding: "0.25rem 0.5rem" }} variant="outline-secondary" onClick={(ev) => { ev.stopPropagation(); handleDown(x) }}> <i className="bi bi-arrow-down"></i> </Button>)} <Button as='div' size="sm" variant="outline-danger" onClick={(ev) => { ev.stopPropagation(); removeFromList(x) }}> <i className="bi bi-x-lg"></i> </Button> </ButtonGroup> </div> </Accordion.Header> <Accordion.Body> {x.blockType != "Image" ? ( <Form.Control as="textarea" value={content} onChange={(ev) => { setContent(ev.target.value); setNotAligned(true); }} ></Form.Control> ) : ( <div> {images.map((image) => ( <label key={generatelocalId()} style={{ paddingRight: "20px" }}> <input value={image.url} onChange={(ev) => { setContent(ev.target.value); setNotAligned(true); }} checked={content == image.url} type="radio" /> <img className="img-box" src={image.url} alt={image.alt} /> </label> ))} </div> )} </Accordion.Body> </Accordion.Item> ); - Assistant
It looks like the useEffect is causing your form to re-render while typing, which makes it difficult to continue typing. Instead of using the
notAlignedstate anduseEffect, you can update the parent state directly in thehandleContentChangefunction.Here's the modified
handleContentChangefunction that you can use:const handleContentChange = (ev) => { const newContent = ev.target.value; setContent(newContent); props.setList((oldList) => { const newList = oldList.map((item) => (item === x ? { ...item, content: newContent } : item)); return newList; }); };And you can update your form controls to use the
handleContentChangefunction as theonChangehandler:{x.blockType != "Image" ? ( <Form.Control as="textarea" value={content} onChange={handleContentChange} ></Form.Control> ) : ( <div> {images.map((image) => ( <label key={generatelocalId()} style={{ paddingRight: "20px" }}> <input value={image.url} onChange={handleContentChange} checked={content == image.url} type="radio" /> <img className="img-box" src={image.url} alt={image.alt} /> </label> ))} </div> )} - User
It looks like the useEffect is causing your form to re-render while typing, which makes it difficult to continue typing. Doing like that will make me able to continue typing? because I see it re-render the list anyway
- Assistant
You're correct that the method I suggested will still re-render the list and could potentially cause a performance issue. In this case, we can use a different approach.
Instead of updating the parent state on every change, you can update it when the form control loses focus (using the
onBlurevent) or when a radio button is clicked. This reduces the number of re-renders.First, update the
handleContentChangefunction:const handleContentChange = () => { props.setList((oldList) => { const newList = oldList.map((item) => (item === x ? { ...item, content: content } : item)); return newList; }); };Then, update the form controls to use the
onChangeandonBluror events: - User
Yes but when I clikc another button in the page it will trigger only the onBlur action. try a different approach, without using ref or anything like that
- Assistant
- User
Yes but it will udpate only the current block and not the others block when I handle up or down. Try a differnt approach
- Assistant
A possible approach is to update the parent state right after clicking on the up or down button, which doesn't require form submission or onBlur, and only updates the content when the page structure is changed: