I am trying to refactor my existing code to use Hooks instead of constructors, componentDidMount(), componentDidUpdate(). My original code below did this just fine:
import React from "react";
import '../css/App.css';
import 'jquery/dist/jquery.js';
import Isotope from 'isotope-layout/dist/isotope.pkgd.min.js';
import projectsData from '../data/projectsData';
import Project from './Projects';
class App extends React.Component {
constructor(props){
super(props);
this.state = {
isotope: null,
projectComponents: projectsData.map(project => <Project id={project.id} project={project}/>),
}
}
componentDidMount() {
//load isotope masonry view
const elem = document.querySelector('#visContainer')
if (!this.state.isotope) {
this.setState({
isotope: new Isotope( elem, {
//options
itemSelector: '.gridItem',
layoutMode: 'masonry',
masonry: {
columnWidth: 140,
fitWidth: true
},
getSortData: {
year: '.yearsstart parseFloat',
kind: '.kind'
}
})
});
}
else {
this.state.isotope.reloadItems();
}
}
}
componentDidUpdate() {
if (this.state.isotope) {
this.state.isotope.reloadItems();
this.state.isotope.layout();
}
}
render(){
return (
<body>
<div id='sepLine'>
<div id="visHolder">
<div id="visContainer" style={{position: "relative", width: "840px", height: "1823px"}}>
{this.state.projectComponents}
</div>
</div>
</div>
</body>
);
}
}
export default App
However, I am trying to amend this to use d3 library instead of mapping an outside component (Projects). And do this with React Hooks. What I have below maps the data using d3 but I am unable to apply Isotope to the id "visContainer". I suspect it is because they are happening at the same time as they are both using useEffect()?:
import React, { useEffect, useState } from "react";
import '../css/App.css';
import 'jquery/dist/jquery.js';
import Isotope from 'isotope-layout/dist/isotope.pkgd.min.js';
import projectsData from '../data/projectsData';
import Project from './Projects';
function App () {
const [isotope, setIsotope] = useState(null);
useEffect(() => {
const projects = d3.select('#visContainer');
projects
.selectAll('.lineDiv')
.data(projectsData)
.enter()
.append('div')
.attr('id', (d,i) => {
return d.title })
.attr('className', d => {
return 'lineDiv gridItem y' + d.start.substring(0,4) + ' ' + d.kind + ' ' + d.title + ' ' + d.subtitle.replace(/, /g, ' ');
})
}, []);
useEffect(() => {
const elem = document.querySelector('#visContainer')
setIsotope(new Isotope( elem, {
itemSelector: '.gridItem',
layoutMode: 'masonry',
masonry: {
columnWidth: 140,
fitWidth: true
},
getSortData: {
year: '.yearsstart parseFloat',
kind: '.kind'
}
})
)
},[]);
return (
<body>
<div id='sepLine'>
<div id="visHolder">
<div id="visContainer" style={{position: "relative", width: "840px", height: "1823px"}}>
</div>
</div>
</div>
</body>
);
}
export default App
Any help would be really appreciated as I am keen to learn how far I can push React Hooks. Thanks in advance!
I worked it out by using "useEffect" on intial load to setProjects() with mapping. Then Isotope takes control of the '#visContainer' with
document.querySelector('#visContainer')
const App = (props) => {
const [projects, setProjects] = useState(null);
const [isotope, setIsotope] = useState(null);
useEffect(() => {
setProjects(projectsData.map(project => <Project id={project.id} project={project} onClick={filterThisProject}/>))
},[])
useEffect(() => {
const elem = document.querySelector('#visContainer')
setIsotope(new Isotope( elem, {
itemSelector: '.gridItem',
layoutMode: 'masonry',
masonry: {
columnWidth: 140,
fitWidth: true
},
getSortData: {
year: '.start parseFloat',
kind: '.kind'
}
})
)
},[projects]);
return (
<body>
<div id='sepLine'>
<div id="visHolder">
<div id="visContainer" style={{position: "relative", width: "840px", height: "1823px"}} >
{projects}
</div>
</div>
</div>
</body>
);
}
export default App;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.