Problem is that I want use Redux and based on the edit toggle (product.editable
) it should be checked if the text are editable.
But everything goes well until it reaches the point that I want to edit the value that is inside the input field.
I need someone who could help me to the right direction. I know the JSON cannot be modified directly, but how can I change the current value and then make it editable inside the input field. Keep in mind I am using dumb components.
Component:
let getProductList = productList.map((product, i) => {
return (
<div key={i}>
{product.editable ?
<div>
<input name="title" ref={(input) => title = input} value={product.title}/>
<input name="description" ref={(input) => description = input} value={product.description}/>
</div>
:
<div>
<div>{product.title}</div>
<div>{product.description}</div>
</div>}
<ProductButtons productId={product._id} index={i}/>
</div>
)
});
export const ProductButtons = ({ productId, index}) => {
const dispatch = useDispatch();
return (
<div>
<button onClick={() => dispatch(deleteItem(productId, index))}>Delete</button>
<button onClick={() => dispatch(editItem(productId, index))}>Edit</button>
<button onClick={() => dispatch(addItem(productId))}>Add</button>
</div>
)
}
Reducer:
case types.UPDATE_PRODUCT: {
console.log('update')
return {
...state,
products: state.products.map((product, i) =>
i == action.index
? {
...product,
editable: product.editable == true ? false : true,
}
: product
),
}
}