Created
September 1, 2020 18:07
-
-
Save manashcse11/1e4a021b9da1315b5490e70f93dc2bba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState } from 'react'; | |
import './App.css'; | |
import { connect } from 'react-redux'; | |
import { addUserData } from './redux/actions'; | |
const App = ({ user, addUserData }) => { | |
const [name, setName] = useState(user != null ? (user.hasOwnProperty('name') ? user.name : '') : ''); | |
const [profession, setProfession] = useState(user != null ? (user.hasOwnProperty('profession') ? user.profession : '') : ''); | |
const handleChange = (event) => { | |
if(event.target.name == "name"){ | |
setName(event.target.value); | |
} | |
if(event.target.name == "profession"){ | |
setProfession(event.target.value); | |
} | |
}; | |
const handleSubmit = (event) => { | |
addUserData({name, profession}); | |
}; | |
return ( | |
<div className="App"> | |
<h1>React Redux Example</h1> | |
Name: <input type="text" name="name" value={name} onChange={handleChange} /><br/> | |
Profession: <input type="text" name="profession" value={profession} onChange={handleChange} /><br/> | |
<button type="submit" onClick={handleSubmit}>Update</button> | |
</div> | |
); | |
} | |
const mapStateToProps = (state) => { | |
const {user} = state.User; | |
return {user}; | |
}; | |
export default connect(mapStateToProps, { addUserData })(App); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment