javascript - Where are interactions and events written in react?
给我你的怀抱
给我你的怀抱 2017-05-19 10:34:33
0
4
586

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?

给我你的怀抱
给我你的怀抱

reply all(4)
大家讲道理

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.

为情所困

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.

迷茫

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.

阿神

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~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template