Iam trying to create a dropdown component and would like to use that selected option through out my app. The thought is when a user select a Dropdown value then, its state got save in Redux reducer then to use that value for other action. But being a beginner Iam stuck on implementation part.
Note: The dropdown wouldnt have a submit button, just the action of selecting the drop down option.
My code until this stage looks like this:
RoleDropdown.js
class RoleDropdown extends Component {
constructor(props) {
super(props);
this.state = {
value: ''
};
}
...
...
render() {
return (
<div>
<select
onChange={() => this.props.selectedRoleAction()}
name="roles" className="form-control">
<option>Select a Role</option>
<option value="ABC" >ABC</option>
<option value="DEF" >DEF</option>
<option value="GHI" >GHI</option>
</select>
<p>role is: {this.props.activeRole.value}</p> //No value output here
</div>
)
}
SelectedRoleAction.js
const selectedRoleAction = (role) => {
const [value, setValue] = useState("")
setValue({ value: role.target.value })
console.log("event from drop down is " + role) //I cant see any logged value as well
return {
type: "ROLE_SELECTED",
payload: role,
}
};
Where Am I doing wrong? Does the "setValue" can be used in action reducers?
useStateis a function that is used to hold local state. It has nothing to do with redux. There are a lot of issues with your code right now. I highly recommend you check out a youtube tutorial on how to use redux.