I am trying to use ImmutableJS to help maintain state in a React component. I can use it just fine if the state contains an immutable object. But, if the entire state is an immutable object, then it fails whenever I try to setState() with an error that says
TypeError: this.state.get is not a function.
Below is a minimal example to reproduce the error. Every time the increment button is pushed, the count variable should be incremented and re-rendered. Please help me understand why this approach does not work. Thanks!
import React from 'react';
import { Map as ImmutableMap } from 'immutable'
class App extends React.Component {
constructor (props) {
super(props)
this.state = ImmutableMap({ count: 0 }) // entire state is immutable
}
increment () {
const newCount = this.state.get('count') + 1
this.setState(prevState => prevState.set('count', newCount))
}
render() {
return (
<div>
{ this.state.get('count') } // produces error after setState()
<button onClick={this.increment.bind(this)}>Increment</button>
</div>
);
}
}
export default App;