nico
18 Feb 2022
React js
Hello I need to filter props.arrayData with filtering in one functionnal component
export function BurnSegments({ arrayData }) {
function handleChange(event) {
event.preventDefault();
this.setState({ arrayData : arrayData .filter(b => b.key === event.target.value) });
}
But setState is undefined, so what is the proper way of changing arrayData in the props?
Thanks
nico
18 Feb 2022
Best Answer
I made it work by using the useState of the functional component as we can't update props.
with the steps:
By the way I used redux here
export function Sample({ arrayData}) {
const [statearrayData , setStatearrayData ] = useState([]);
useEffect(() => {
// we update here the state with the prop
setStatearrayData (arrayData);
}, [arrayData]);
function handleChange(event) {
event.preventDefault();
setStatearrayData(arrayData.filter(b => b.key === event.target.value));
}
}
function mapStateToProps(state) {
return {
arrayData: state.arrayData
};
}
function mapDispatchToProps(dispatch) {
....
}
export default connect(mapStateToProps, mapDispatchToProps)(Sample);
© 2024 Copyrights reserved for web-brackets.com