Reputation: 181
I am new to React JS and planning to use it in one of my projects. I have gone through the docs and have a slight idea of React. I have a scenario with the following details :
I was going through few articles in stack overflow for appending data dynamically and few of them are below :
All the above solutions doesn't provide a direct option for appending. They recommend to create a map / array and add the values to the map and re-render the values.
I am not very comfortable in using maps as I won't be passing the details to server and its only shown to user. I think creating a map will have redundant values which occupies memory if the list grows huge.
I dont want to mix jQuery / any other library along with React. Is there any possibility where a render function / component will append to DOM instead of maintaining in an array as the items in the list are not similar.
Why there isn't any option to traverse / edit DOM directly instead of maintaining a list. Can you guide me if there is an option so that I will be able to understand its advantages.
Upvotes: 0
Views: 1712
Reputation: 157
Hi I was in the exact situation. Switched from jQuery to React recently. The solution is Redux. React wont have any DOM manipulation whatsoever. So forget about the .parent().child().next() things we had before.
Theres this concept in Redux's core. State management. So basically state is a little tool you need to manipulate through the React 'tree'. Kinda like the DOM manipulation in jQuery but not totally the same. The state will flow between siblings, up to parent or down to child
Upvotes: 1
Reputation: 6132
So just update the data like this when you receive some updates from the server and render will take care about displaying them:
onDataReceived(newData) {
// or you can mutate the `data` array here, doesn't matter too much
this.setState({data: data.concat(newData)})
}
render() {
// render this.state.data
}
Upvotes: 2