2

I'm learning ReactJS and today I can't resolve a difficulty, so I need your help.

I want to make a "Remember Me" for users that want to stay connected after reopening the website.

This is my code :

My function :

handleChangeCheckBox = (event) => {
  console.log(event.target.checked)

  this.setState({
    isChecked: event.target.checked
  })
}

When I call the function in the input checkbox field with an onChange :

<p>
  <Input
    type="checkbox"
    name="rememberCheckbox"
    className="rememberCheckbox"
    id={this.state.isChecked}
    onChange={this.handleChangeCheckBox}
  />
  Remember Me
</p>

Nothing appears in my console when I click on the checkbox, so it seems like the calling function isn't working.

6
  • What Input component is that? Commented Jan 16, 2020 at 10:58
  • It's a reactstrap Input Commented Jan 16, 2020 at 10:58
  • Look like you are using UI kit, or you should change <Input/> to <input/> Commented Jan 16, 2020 at 10:59
  • Please refer link for this issue stackoverflow.com/questions/26615779/… Commented Jan 16, 2020 at 10:59
  • check this codesandbox.io/s/rememberme-input-3rz12 Commented Jan 16, 2020 at 11:01

5 Answers 5

6

please try this. replace value with checked

<input type="checkbox" 
    name="rememberCheckbox"
    checked={this.state.isChecked}
    onChange={this.handleCheckBox}
/>
Sign up to request clarification or add additional context in comments.

Comments

1

Code tested at given url enter link description here

 class App extends Component {
  constructor(props) {
    super(props);
    this.state = { isChecked: false };
  }

  handleCheckBox = event => {
    console.log(event.target.checked);

    this.setState({
      isChecked: event.target.checked
    });
  };

  render() {
    return (
      <div>

       <p>
          <input
            type="checkbox"
            name="rememberCheckbox"
            value={this.state.isChecked}
            onChange={this.handleCheckBox}
          />
          Remember Me
        </p>
      </div>
    );
  }
}

2 Comments

Hello, thanks for your reply. Your solution isn't working, but thanks for your help !
Your code is working at the url you gave but on mine, when I correct everything it doesn't work
0

Welcome to Stack overflow!

Based on the code you shared, your function should be getting executed. I suspect you have an error somewhere else in your class rather than in those two code snippets you shared.

You can check this simple sandbox I made with a very barebones implementation like the one you have: https://codesandbox.io/s/react-simple-check-box-7etp4

One thing you're missing is using the checked html attribute rather than id. That's what will tell the checbox whether it's checked or not. It replaces the value attribute you use for input fields.

You also seem to be using an Input component. Try changing it for a normal HTML input instead.

Comments

0

it could be triggering twice.

if you are using create-react-app then your Appcomponent is wrapped in StrictMode like this:

<React.StrictMode>
  <App />
</React.StrictMode>,

go to index.js and remove <React.StrictMode></React.StrictMode>

https://github.com/facebook/react/issues/12856#issuecomment-390206425

Comments

0

handleChangeCheckBox = () => { this.setState({ isChecked: ! this.state.isChecked }) }

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.