Home > front end >  How can I create a new component pressing a Button in React Native?
How can I create a new component pressing a Button in React Native?

Time:09-22

I'm stuck in a problem, I have to press (onPress) a button and this must create a new component. Just like a to-do list. But, I don't know how can I solve this problem.

CodePudding user response:

There's lots of todo apps you could look at to get a better idea, but generally the solution is a component with a list on state which renders an element for each item in the list.

const Comp = () => {
  const [todos, setTodos] = useState(["first"]);

  return <>
    {todos.map(t => <Row
      onPress={() => setTodos([...todos, Math.random().toString()])
     >
      {t}
    </Row>)}
  </>;
};
  • Related