score:1
you don't need to make a function to change state
const [eligible,seteligible] = react.usestate(
false // default value
)
to update state state
seteligible(true)
score:0
i get the impression that you read two different react approaches and mixed them in your code: the old class-based (without hooks but with an explicit state), and the hook based. with hooks you don't really have a state, and as fahad pointed out you can just set individual (state-)variables directly. if you want to keep with the object you've got, you'd do:
function seteligibility() {
setstate({uneligible: true});
}
score:0
the state.uneligible==='true'
is a condition, instead of assignment.
you should change the below line:
function seteligibility(){state.uneligible==='true'}
to
function seteligibility(){state.uneligible = 'true'}
score:0
you are declaring the state variable name as state which is not a good practice and might throw a keyword error as well. instead, use the variable name as eligible.
define the state like this:
const [uneligible, setuneligible] = react.usestate(false);
condition for setting it would change to this:
var split_dob = dateofbirth.split('-');
var dob_asdate = new date(split_dob[0], split_dob[1], split_dob[2]);
var today = new date();
var mili_dif = math.abs(today.gettime() - dob_asdate.gettime());
var age = mili_dif / (1000 * 3600 * 24 * 365.25);
if (age < 18) {
setuneligible(true);
}
if uneligibile is set to false, you are rendering an empty grid which does not seem to be okay. rather:
if (uneligible) {
return (
<grid item xs={8} style={{ margintop: '10px' }}>
<alert variant="filled" severity="error">
this is an error alert — check it out!
</alert>
</grid>
);
} else {
return null;
}
Source: stackoverflow.com
Related Query
- how to set the state of useState hook to true
- How to set the state of a useState hook on page load?
- How to set an initial state using the useState hook
- How to set initial state for useState Hook in jest and enzyme?
- useState hook can only set one object at the time and return the other object of the same array to initial state
- How to set initial state value for useState Hook in jest and enzyme?
- useState react hook does not set the array state
- How to update state with the useState hook
- How to set the current state to the useState hook?
- How to make functions that change the state via useState hook reusable?
- How to set several state at the same time with useState hook?
- How to set a state in a forEach loop using useState hook
- How to set the initial value of useState with the value of another state set in a useEffect hook?
- How can I dynamically set the type of state inside a custom hook
- How do I increment the count of a selected index of state object using setState method given by useState hook in my react application
- How to set initialstate from user input inside the useState hook
- How to use useState hook with array state for children that call the setter function without infinite rendering loop?
- How to asynchronously set state in useState hook React
- How to fix map is not function even there is already set data in the useState using React Hook
- React - weird behavior when useState hook sets state for the first time vs subsequent times
- How can I use the useQuery hook to populate state in other hooks?
- How to TEST async calls made in componentDidMount that set the state of React Component
- How to use React Context with useState hook to share state from different components?
- How do you set the Typescript type for useRef hook in React Native?
- Set multiple state values with React useState hook
- Better way to use useState hook for setting the boolean state in React
- how can we use redux state in useState to set initial values
- How to use the useState hook with asynchronous calls to change an array?
- Why does calling the set function of a useState hook apply immediately in async functions?
- How to set state from props that is passed to the component in react?
More Query from same tag
- How to create a progress bar in React?
- Unable to add css animation property to component
- React - Setting state to target with onClick method
- Objects are not valid as a React child when trying to map antd Select options
- reactjs storing state in constructor vs property
- setState update array value using index in react
- Toggling Issue React JS
- Creating div in function (React)
- How can I pass configuration options to yarn/npm
- ReactJS rendering issue
- Switching pages with history.push() passing state - React Router 5
- code for fetch text box value button click using react js
- Typescript addEventListener to keydown giving error
- How to put raw html stored in variable inside a react component
- Download File automatically with browser that can be opened or read properly
- Uncaught TypeError: Cannot read properties of undefined (reading 'source') react v16
- Add data to a react component
- React Change Username
- React client side Google api Calendar 403
- Multithreaded Request With Javascript
- I want to use bootstrap class and next.js modules together inline
- What are refs in React?
- Can I do the server rendering with data in React?
- UseEffect uses old value of changed state
- How to have a one whole file as a router file in react
- React Component onClick do Something depending on prop Type
- How to exclude mobx and mobx-react from the bundle using webpack
- Multi-page Applications with Meteor/React
- create react app not picking up .env files?
- Allow both CSS modules and imports