update 2:
hi,
- sorry I forgot to mention about the api call in my question earlier
- I making an api call in redux way.
- so in my actions/index.js file I am calling my api in getSports method.
- but the problem is when I try to retrieve the values I am getting an error.
A cross-origin error was thrown. React doesn't have access to the actual error object in development. See b.me/react-crossorigin-error for more information.
- so I debugged the props in the class AsyncValidationForm
console.log("AsyncValidationForm this.props---->", this.props);
- there I dont see the getSports method.
- can You tell me how to retrieve the values so that I can assign the them to my radio button label.
https://codesandbox.io/s/yv1zpj874x
actions/index.js
import axios from "axios";
export function testData() {
let response = { data: "test" };
return {
};
}
export function getSports() {
return dispatch => {
axios
.get("https://jsonplaceholder.typicode.com/users") //works
.then(response => {
})
.catch(error => {
});
};
}
AsyncValidationForm.js
import * as actions from "../actions";
class AsyncValidationForm extends React.Component {
constructor(props) {
super(props);
console.log("AsyncValidationForm this.props---->", this.props);
this.state = {
//pass down VerticalLinearStepper.js state if any
username: this.props.username ? this.props.username : "",
password: this.props.password ? this.props.password : "",
//this determines whether any fields is filled or not from VerticalLinearStepper
pristine:
this.props.username || this.props.password || !this.props.disabledNext
? false
: true
};
}
radio button label in AsyncValidationForm.js
<label>
<Field
name="sex"
component={renderField}
type="radio"
value="male"
checked={!this.props.disabledNext}
onChange={this.passRadioValue}
/>{" "}
Male
</label>
- I am new to redux form.
- I am trying to enable the button after I click the radio button.
- to enable the go to next step button I tried setting up state and created new onclick method in the radio button.
- but still its throwing an error,
- not sure how to pass the radio button values to enable the button.
- Can you tell me how to fix it, so that in future I will fix it myself.
- Providing my sandbox and relevant code snippet below.
update1: now only this error has been removed https://codesandbox.io/s/4jpkk394x7?moduleview=1
https://codesandbox.io/s/pjj6m1l9pq
AsyncValidationForm.js
const AsyncValidationForm = props => {
console.log("AsyncValidationForm ---->");
const { handleSubmit, pristine, reset, submitting } = props;
// this.state = {
// disabled: false
// };
// this.setState({ disabled: !this.state.disabled });
const passRadioValue = (e) =>{
}
return (
<form onSubmit={handleSubmit}>
<Field
name="username"
type="text"
component={renderField}
label="Username"
/>
<Field
name="password"
type="password"
component={renderField}
label="Password"
/>
<label>
<Field name="sex"
component={renderField} type="radio" value="male"
onClick={this.passRadioValue("right")} />{" "}
Male
</label>
<div>
<button type="submit" disabled={submitting}>
Sign Up
</button>
<button type="button" disabled={pristine || submitting} onClick={reset}>
Clear Values
</button>
</div>
</form>
);
};
StepTemplate.js
<Button
variant="contained"
color="primary"
onClick={onNext}
className={classes.button}
>
{canGoBack ? "Next" : "go to next step"}
</Button>
https://codesandbox.io/s/6zrw7r66rr
I have forked your codesandbox, and edit 4 files. Pretty sure it satisfies all your requirements stated above
VerticalLinearStepper.js: this is where we store our
username, password, disabledNext (radioButton)
state andhandleChange
method forsetState
. Then, we passed down the state to ->Step1.js
->AsyncValidationForm.js
.In
AsyncValidationForm.js
, we bindonChange
method to track the value and call thesetState
method andthis.props.handleChange
forsetState
inVerticalLinearStepper.js
Then, in
StepTemplate.js
AdddisabledNext, checkDisabledNext
props.checkDisabledNext
to determine whether the Next Button will have conditional checking or not.disabledNext
is the disabled value.This is
Step1.js
, here we just pass props toStepTemplate
andAsyncValidationForm
:Here is the re-render issue fix: https://codesandbox.io/s/vqvxj7ky4y Update
VerticalLinearStepper.js
, then we dont need Step1.js file anymore, since we write the content of Step1.js in this file:Additional reference: React: Class Component vs Functional Component
First thing you will need to do is remove the reference to
this
from your functional component...onClick={this.passRadioValue("right")}
TO
onClick={passRadioValue("right")}
Functional components inherit their functional context from the parent function's scope and don't have a
this
object. this will remove the immediate errorsHere's is a forked version or your codepen I've started for reference...
https://codesandbox.io/s/4jpkk394x7?moduleview=1
And some background on Functional (stateless) vs Class (stateful) components...
https://programmingwithmosh.com/react/react-functional-components/
This is a simple code to enable a button on radio button click If you want a more descriptive one please elaborate your question