0
\$\begingroup\$

Attempting to write a simple Kanban board in ReactJS. Omiting the css code, mostly wanted some advise on best practices in the ReactJS code itself.

export default function App() {
 const [todo, setTodo] = React.useState(["finish xyz", "finish abc"]);
 const [inprogress, setInprogress] = React.useState(["hello world"]);
 const [done, setDone] = React.useState(["hey there!"]);
 const moveLeft = (type, taskname) => {
 if (type === "inprogress") {
 setInprogress([...inprogress.filter((val) => val !== taskname)]);
 setTodo([...todo, taskname]);
 } else if (type === "done") {
 setDone([...done.filter((val) => val !== taskname)]);
 setInprogress([...inprogress, taskname]);
 }
 };
 const moveRight = (type, taskname) => {
 if (type === "todo") {
 setTodo([...todo.filter((val) => val !== taskname)]);
 setInprogress([...inprogress, taskname]);
 } else if (type === "inprogress") {
 setInprogress([...inprogress.filter((val) => val !== taskname)]);
 setDone([...done, taskname]);
 }
 };
 return (
 <div className="board">
 <div className="todo">
 <div>TODO</div>
 {todo.map((curr) => (
 <Task
 key={curr.replaceAll(" ", "-")}
 moveLeft={moveLeft}
 moveRight={moveRight}
 type="todo"
 taskName={curr}
 />
 ))}
 </div>
 <div className="inprogress">
 <div>INPROGRESS</div>
 {inprogress.map((curr) => (
 <Task
 key={curr.replaceAll(" ", "-")}
 moveLeft={moveLeft}
 moveRight={moveRight}
 type="inprogress"
 taskName={curr}
 />
 ))}
 </div>
 <div className="done">
 <div>DONE</div>
 {done.map((curr) => (
 <Task
 key={curr.replaceAll(" ", "-")}
 moveLeft={moveLeft}
 moveRight={moveRight}
 type="done"
 taskName={curr}
 />
 ))}
 </div>
 </div>
 );
}
function Task(props) {
 return (
 <div className="task">
 <div>{props.taskName}</div>
 <button onClick={() => props.moveLeft(props.type, props.taskName)}>
 left
 </button>
 <button onClick={() => props.moveRight(props.type, props.taskName)}>
 right
 </button>
 </div>
 );
}
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Aug 29, 2022 at 3:41
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
  1. Consider use of useReducer instead of multiple state variables. It helps keep component clean by moving all this transition logic outside of the component.
  2. Extract types ("inprogress", "todo", "done") as constants. It's easy to maintain all these magic string when they are constants.
  3. filter creates copy of array, spreading is unnecessary.
  4. There are no restrictions on key value, it can contain spaces.
  5. I suggest to pass click handlers to the Task component to make it dumber.
answered Aug 29, 2022 at 11:22
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.