0

I made a simple ToDo List in React.

I have a component for Adding a new ToDo item (eg. name, title, date, place, description), and another component for Editing a ToDo item.

The 2 components however, are exactly the same, except that the Edit component is filled with content.

Is there I way I can simplify this, eg. nest a "general form" component for both the Edit and Add Component? And should I be looking into higher order components?

1 Answer 1

1

You can make a renderedTodo component and pass property isEditing, for example. And inside render function pick proper component to render

class TodoItem extends React.Component {
    render() {
        const renderedTodo = this.props.isEditing ? (<EditingTodo>) : (<AddingTodo>);
        return (
            <div>
                <div>[Common structure]</div>
                { renderedTodo }
            </div>
        );
    }
}

TodoItem also manages all common logic, EditingTodo and AddingTodo only logic related to them. They should be pure functions without any state and do everything using received props from TodoItem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.