score:1
as much as i understand ur question, you can do something like this https://codesandbox.io/s/happy-swartz-ikqdn?file=/src/clickinput.js
- you can have a
items
andsetitems
state and functions in the parent component. - you can do to save the value of a function is
function changeitem(i, value) { //index to be updated and value
let newitems = [...items];
newitems[i] = value;
setitems(newitems);
}
- there will a component to map over and render the list
function list({ items, setitem }) {
return (
<>
{items?.map((itm, i) => (
<label
key={i}
title={itm}
onchange={(e) => setitem(i, e.target.value)} //pass the index and value of the edited index to be saved
/>
))}
</>
);
}
- for each label, you can create a component having an
edit
state, which is triggered when clicked.
function label({ title, onchange }) {
const [edit, setedit] = usestate(false);
return (
<div>
{!edit ? (
<span onclick={() => setedit(true)}>{title}</span> //onclick to start edit
) : (
<>
<input autofocus value={title} onchange={onchange} /> //input to edit
<button onclick={() => setedit(false)}>save</button> //this is to toggle edit
</>
)}
</div>
);
}
Source: stackoverflow.com
Related Query
- How to import components in React and edit them (Change their State)
- How to import and export components using React + ES6 + webpack?
- how and when to call a react component methods after state change from redux
- How to optimize React components with React.memo and useCallback when callbacks are changing state in the parent
- How to properly store React components in separate files and import React?
- How to properly write types for an array of generic React components and their properties?
- how to import multiple components as a single component and use them in lazy load
- React How to change state value from child to Parent using useState and useContext
- How to handle the state of multiple inputs and change their state from the parent?
- How to pass down state and handlers in nested React components
- How to identify React components with the useInView hook and pass their values to a child component?
- How to keep the common React Components fixed in all pages instead of calling them again and again in each page
- How to wait for redux state change and then render page in React
- How to import scss files and use them globally in a React application?
- how to change state of 1 out of 3 components in react
- React: Trying to create separate folder for SVGs and import them into my components but unsure of how to import
- How to edit a React component and change the text displayed on screen
- How can I get focused element and change state in React component?
- REact js noob here, how do you change state object values while looping thru an array with their keys in it
- How to unit test an api call and assert state change in react and enzyme?
- How do I test methods of React components and include them in my Istanbul coverage?
- How to refresh onscreen data from state change in react and redux
- My State Does Change But Components Are Disappear When I Update My State In React Native How To Fıx This?
- How to use enzyme and jest to detect change React state
- How to make a react class component retain its state after login and change available routes based on the state currently available in the component?
- How to set sorting column state as "asc" after click on new column and change sorting state "asc": "desc" during clicking on selected column in React
- React how to make children components have their own state
- How to render react components by using map and join?
- How to import CSS modules with Typescript, React and Webpack
- How to detect when a image is loaded, that is provided via props, and change state in React?
More Query from same tag
- Why is functional component nested function using a stale state value?
- nested lists using map firing multiple onClick event listeners
- How to get numeric value from Reanmited Value?
- How do I test this?
- Redux-Form separating displayed value and submitted value in custom component
- Webpack externals dependency
- React TextArea value not automatically updating as expected
- Mapbox map is getting loaded before useEffect returns data
- Highlight menu icon when on that page using React/ Gatsby?
- React onChange requires double click to get selected value
- extending React.Component vs Component
- DOM Manipulation With `useEffect()` vs. `useLayoutEffect()`
- React/Redux download file
- How do I use the react-admin's SelectArrayInput element to send data to the server as an array of objects i.e. { clients: [id: 1, id: 2]}?
- Prevent top scroll after update state
- How to add a fixed marker on a Mapbox map? (React)
- How can I convert a class with a constructor to a functional component using React Hooks?
- dividing function to a separate file
- Retrieve custom data-* attributes value in React Component(without event)
- React-Table doesnt show data from Firebase
- how to use map function with (axios/classhooks) to read from API
- How to force SonarLint to scan jsx files?
- React.PureComponent renders all the items in a list component
- Click on React list of elements and store it as a value in another list
- How to re-fetch MockProvider before a "fireEvent" from @testing-library/react?
- Nested props in styled-components giving Typescript error
- React with Redux: Module build failed: SyntaxError: Unexpected token <
- React partial rerender tree
- What does 'Only a ReactOwner can have refs.' mean?
- Update list when redux store changes