I have a JSON file:
[
{
"id": 1,
"img": "https://example.jpg",
"availability": false
},
{
"id": 2,
"img": "https://example.jpg",
"availability": true
}
]
What I would like to achieve is for the JSON with availability : false
to automatically have a filter applied to it. This is the filter I want to apply and I would like to apply it to the img with the className="tile-image"
:
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
This is my code so far:
import React, { Component } from 'react';
import './styles.css'
class GetOnlinePosts extends Component {
constructor(props){
super(props);
this.state = {
error : null,
isLoaded : false,
posts : []
};
}
componentDidMount(){
fetch("https://api.myjson.com")
.then( response => response.json())
.then(
(result) => {
this.setState({
isLoaded : true,
posts : result
});
},
(error) => {
this.setState({
isLoaded: true,
error
})
},
)
}
render() {
const {error, isLoaded, posts} = this.state;
const orderedPosts = [...posts.filter((post) => post.availability), ...posts.filter((post) => !post.availability)]
if(error){
return <div>Error in loading</div>
}else if (!isLoaded) {
return <div>Loading ...</div>
}else{
return(
<div>
<div className="tiles">
{
orderedPosts.map(post => (
<div key={post.id}>
<div className="tile">
<img className="tile-image" src={post.img} alt=""/>
</div>
</div>
))
}
</div>
</div>
);
}
}
}
export default GetOnlinePosts;
Any help on how to get the filter on to the JSON with a false availability would be great.