search

Home  >  Q&A  >  body text

javascript - Where are interactions and events written in react?

Beginners to react still use jquery to handle events as before
Suppose I use index.js to organize some components a.js b.js
I need to implement ajax to load images in a certain component
Where should this event be mounted? compnentDidMount?
Do all events need to be mounted on the component itself?
Is the component life cycle function designed to mount various events?
Do not write any events in index.js except render?

给我你的怀抱给我你的怀抱2748 days ago594

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-19 10:36:33

    As the name suggests, the life cycle allows you to do something at various stages of the component. Calling ajax is one of them, but it is not the only one.

    Under the premise of not introducing Redux, try not to call ajax in the child component, and place it in the highest parent component as much as possible.

    render is the rendering function, and the event processing function is mounted on jsx.

    You can also wait for the component to render and then use native js to add events to the component. This is no problem.

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:36:33

    If it is just an event of internal interaction of the component, then your ajax loading event can be written to the componentDidMount component rendering completion.

    reply
    0
  • 迷茫

    迷茫2017-05-19 10:36:33

    The render in react is re-rendered by its own judgment. As long as the value in your state changes, it will re-judge which places need to be updated.
    For interaction, you can write onClick events normally, remember When writing, pay attention to the problem of this pointing, and it is recommended to write with arrow functions.
    In addition, if some functions want to avoid repeated binding, they can be bound uniformly in the constructor of the component.

    reply
    0
  • 阿神

    阿神2017-05-19 10:36:33

    I need to implement ajax loading of images in a component
    Where should this event be mounted? compnentDidMount?
    Answer: It depends on how you load it. If you load the image through an event, then bind the event directly to the component, send a request, and use state to control whether the component is displayed. If the page is loaded during initialization, then just send the request directly to componentDidMount.
    Is the component life cycle function designed to mount various events?
    Answer: Different methods will be triggered at different stages of the component life cycle. It may be to send a request, or to change the state, not necessarily~
    Do you not write any events in index.js except render?
    Answer: index is the entry file~

    reply
    0
  • Cancelreply