I am creating an app with React/Redux, I have successfully managed to create an action/reducer to insert a record into my database - which is ForerunnerDB
My code so far looks like this:
PAGE
class Projects extends Component {
componentWillMount() {
this.props.FetchProjects();
}
renderProjects() {
return this.props.projects.map( ( project ) => {
return (
<li key={ project._id } >
<Link to={ `/pages/${project._id}` } >{ project.title } { project.timestamp.toString() }</Link>
</li>
);
});
}
render() {
return (
<div className="projects container">
<div className="projects">
<ul>
<li>
<CreateNewProject />
</li>
{ this.renderProjects() }
</ul>
</div>
</div>
);
}
}
function mapStateToProps( state ) {
return {
projects: state.projects.all
}
}
export default connect( mapStateToProps, actions )( Projects );
I am using the <CreateNewProject /> component to insert the record and the renderProjects() method to fetch the list of projects. The projects are rendered just fine if I refresh the page, but I cannot figure out to get the list to update at the point a record is inserted into the DB automatically.
Can anyone help point me in the right direction?
Many thanks in advance.
EDIT
PROJECTS actions
// Fetch existing projects
export function FetchProjects() {
return ( dispatch ) => {
// Load projects collection
projectsCollection.load( ( err ) => {
// if there are no errors
if( !err ) {
// Set up action payload
const projects = projectsCollection.find();
// Dispatch the action using the above
dispatch({
type: FETCH_PROJECTS,
payload: projects
});
// If there's an error loading the projects collection
} else {
//Log the error
console.log( `ERR! ${err}` );
}
});
}
}
// Create new project
export function CreateProject( { projectName } ) {
return ( dispatch ) => {
// Load projects collection
projectsCollection.load( ( err ) => {
// If there are no errors
if( !err ) {
// Set up action payload
// Insert record into projects collection
const projectCreate = projectsCollection.insert({
title: projectName,
timestamp: fdb.make( new Date() )
}, ( result ) => {
// If insertion is successful
if( result.inserted.length ) {
// Save the projects collection (persisted to local storage)
projectsCollection.save( ( err ) => {
// If there are no errors
if( !err ) {
console.log( result.inserted[0] )
} else {
// Log the error
console.log( `ERR! ${err}` );
}
});
} else {
// Log the failed insertion
console.log( `ERR! ${result.failed}` );
}
});
// Dispatch the action using the above
dispatch({
type: CREATE_PROJECT,
payload: projectCreate
});
// If there's an error loading the projects collection
} else {
// Log the error
console.log( `ERR! ${err}` );
}
});
}
}
FETCH_PROJECTS reducer
// Import dependencies
import { FETCH_PROJECTS } from '../../actions/types';
// Set inital state
const INITIAL_STATE = { all: [] }
export default function( state = INITIAL_STATE, action ) {
switch( action.type ) {
case FETCH_PROJECTS:
return { ...state, all: action.payload }
}
return state;
}
CREATE_PROJECTS reducer
import { CREATE_PROJECT } from '../../actions/types';
export default function( state = {}, action ) {
switch( action.type ) {
case CREATE_PROJECT:
return { ...state, projects: action.payload }
}
return state;
}
this.props.FetchProjects()to get data up to date. That's occurs because you are usingpropswhich dont triggerrendermethod, instead ofstate