score:1
you're pretty much making a form with a few rows of multiple choice answers.
one way of simplifying everything is to have all the logic in the top component, in your case i think mainpage
would be where it would be. pass down a function as a prop to all the descendants that allows them to update the form data upstream.
score:1
in q2, how do intend to check the state for each row? perhaps you can use arrays or objects to keep track of the status of each question. the arrays/objects are stored in state, and you just check them to see what the status is.
i'm actually not clear what your app looks like - what does a row look like? (you might want to post a screenshot) and i don't see any way for rank to be selected - i don't even see what the ranks are for, or how they are used in the form. so perhaps your form design needs to be tweaked. you should begin the form design with a clear picture in your mind about how the app will work. maybe start by drawing the screens on paper and drawing little boxes that will represent the objects/array variables and go through the process of a user using your app. what happens to the various boxes when they click radio buttons and so on. how will you know if the same rank is selected twice - where are the selected ranks stored? what animals are clicked/selected? where are those stored? draw it all on paper first.
array or objects: if you want to keep it simple, you can do the whole project just using arrays. you can have one array that stores all the animals. you can have a different array that stores which animals are selected right now (use .includes() to test if an animal is in that array). you can have another array that stores the rows that have a rank selected. when the number of elements in that row === the number of rows (is that the same as the number of animals? if yes, then you can use the length of the animals array for that test)
how do you know if the rows with a rank selected are unique? one way is to disallow selected a rank that has already been selected. again, use .includes() (e.g. arrselectedranks.includes(num)
) to check if a rank has already been selected.
so what do one of these checks look like?
const handleanimalselect = (animal) => {
const err = this.state.selectedanimals.includes(animal);
if (err === true){
this.setstate(error: animal);
}
}
return (
<input
type="radio"
name={this.props.animalname}
value={i}
onclick={this.handleanimalselect}
/>
{ error !== undefined && (
<div class={style.errormessage}>{`animal ${} was chosen twice`}</div>
)}
);
};
remember: state is what you use for remembering the value of variables in a given component. every component has its own state. but props are for data/functions/elements that are passed into the component. you don't update the values of props in the component (prop values are stored in another component. if you need to update them, you use functions to pass data back to the parent component where that variable is in state, and update the value there).
here is an example that uses .includes()
to check for the presence/absence of something:
Source: stackoverflow.com
Related Query
- React Matrix of Radio buttons
- Uncheck radio buttons in react
- how to load radio buttons dynamically and use check property in react js?
- Is there any way to make React Material-UI radio buttons required
- React controlled radio buttons not being checked
- Radio Buttons with Styled Components for React
- True and false radio buttons in react
- React TypeScript: Radio buttons value onChange
- managing the state of looped radio buttons in react
- React checkbox input for replacement to radio buttons
- How to implement React Bootstrap Radio Buttons
- React with bootstrap and Radio buttons
- Radio buttons on a React component take an extra click to be marked
- Radio Buttons in React using Bootstrap always post same value
- Toggle checked state for an array of radio buttons React
- In D3 - sync'ing up two sets of radio buttons and updating React State
- Radio button validation in React for dynamic number of radio buttons
- react hook form: how can i validate a group of radio buttons or checkboxes to ensure at least one is selected
- How to use radio buttons in react boostrap
- Radio buttons in a Semantic UI React table retain checked attribute after selecting another radio button in the same row?
- Create radio buttons in React
- HI! I'm new in react and trying to make multiple radio buttons
- React use form for img radio buttons
- Filtering content with radio buttons in React
- Creating dynamic radio buttons with React and Meteor and Simple Schema (using pup boilerplate)
- React setting "checked" state on radio buttons
- React Hook radio buttons in map function
- Radio Buttons In React - I Have To Click Twice To See A Selection And Update State
- Setting values from radio buttons generated with map in react js
- Li product cards with Radio buttons conflict - React
More Query from same tag
- Correct way to inline style in JSX
- Assign local state value to redux state value?
- What's the fastest way to mutate a single object property inside a large array of objects?
- React routing within another component V6
- Cant get event.target.value using select item from material-ui autocomplete with onchange
- Passing value from callback in component to another component
- 400 bad request during axios call
- Property 'value' does not exist on type 'never'. when use useRef hook in mui
- How to pass data between to independent component on button click in React JS?
- how to map inside a map in React
- React need to click twice to render data fetched from api
- How to get Material-UI V5 to auto-generate :hover colors in custom color objects for buttons
- addEventListener doesn't work on initial render
- Is there a good way to use private variables and methods in React.js
- Handle Error when the server is off in react js (TypeError Failed to fetch)
- Navigate from react-dom doesn't re-render the entire page
- Get previously saved value from state, after component re-render
- Jest "unexpected token" with Antd and React
- Unhandled Rejection (Error): Cannot assign to read only property 'getPosts' of object '#<Object>'
- TypeError: Cannot read property 'stateUpdate' (Which is a function) of undefined / Passing data from Parent to child
- React.js, Each child in a list should have a unique "key" prop. how to fix if key is already given
- React useState not updating on render
- Firing multiple api calls at once
- Create an array of strings of every lines
- Default value for default generic
- How to set the image on top of the div as constant
- Check if props are undefined when loading component
- Module not found: Can't resolve '../../assets/img-3.jpg' in ''
- deploying spring app with react frontend to heroku
- Cannot read properties of undefined (reading 'length') using reactjs