Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

react checkbox onChange to call function is check state is true in typescript

$
0
0

I am exploring the react onChange feature. The functionality I would like to do iswhen checkbox is selected, I would like to update local data to add some valuewhen checkbox is unselected, I would like to just populate the original datathe code I have seems do the reverse of what I want. any can provide some guideline?

codesandbox

import "./styles.css";import { useState } from "react";export default function App() {  const localData = [{ name: "apple", phone: 12345 }];  const [check, setCheck] = useState(false);  const [data, setData] = useState(localData);  const handleOnChange = () => {    setCheck(!check);    check ? setData([...data, { name: "amazon", phone: 222 }]) : setData(data);  };  return (<div className="App"><h1>Hello CodeSandbox</h1><p><input type="checkbox" check onChange={handleOnChange}></input></p><p>{JSON.stringify(data)}</p></div>  );}

Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>