I am a beginner starting out in react and having difficulties in state management while working with react-redux and useState.
In the below code I am basically following a tutorial which passes in the this.state
to the dispatch action createProject
in the handleSubmit
function as seen below.
class CreateProject extends Component {
state = {
title: '',
content: ''
}
handleChange = (e) => {
this.setState({
[e.target.id]: e.target.value
})
}
handleSubmit = (e) => {
e.preventDefault();
// console.log(this.state);
this.props.createProject(this.state);
}
render() {
return (
<div className="container">
<form className="white" onSubmit={this.handleSubmit}>
<h5 className="grey-text text-darken-3">Create a New Project</h5>
<div className="input-field">
<input type="text" id='title' onChange={this.handleChange} />
<label htmlFor="title">Project Title</label>
</div>
<div className="input-field">
<textarea id="content" className="materialize-textarea" onChange={this.handleChange}></textarea>
<label htmlFor="content">Project Content</label>
</div>
<div className="input-field">
<button className="btn pink lighten-1">Create</button>
</div>
</form>
</div>
)
}
}
const mapDispatchToProps = dispatch => {
return {
createProject: (project) => dispatch(createProject(project))
}
}
export default connect(null, mapDispatchToProps)(CreateProject)
Now, instead of using Class component I am using the functional component and using the useState
hook. in the handleSubmit function how can I pass the state to the this.props.createProject()
as this.props
is not defined in-case of functional component and how to pass the state in place of this.state
, will it be the projectinfo
which stores the state or something else, I'm confused.
TLDR; I want to rewrite the this.props.createProject(this.state)
line from the perspective of useState
hook.
Below is the code I have written:
const CreateProject = () => {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const handleSubmit = (e) => {
e.preventDefault();
const projectinfo = { title, content }
this.props.createProject(projectinfo);
}
return(
<div className="container">
<form onSubmit={handleSubmit} className="white">
<h5 className="grey-text text-darken-3">Create Task</h5>
<div className="input-field">
<label htmlFor="title">Title</label>
<input type="text" value={title} id="title" onChange={(e) => setTitle(e.target.value)} />
</div>
<div className="input-field">
<label htmlFor="content">Content</label>
<textarea id="content" className="materialize-textarea" onChange={(e) => setContent(e.target.value)}></textarea>
</div>
<button className="btn pink lighten-1 z-depth-0">Create</button>
</form>
</div>
);
}
const mapDispatchToProps = (dispatch) =>{
return{
createProject: (project) => dispatch(createProject(project)),
};
}
export default connect(null, mapDispatchToProps)(CreateProject);
Any help or clues to understand this would be appreciated. Thank you!