npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-path

v0.0.2

Published

A way to initial your React page after data loaded

Downloads

19

Readme

React-Path

Initial React Page with original data by AJAX

##Usage If you prefer to use React with your routes in backend. This module could be used to inital your React page after retrieving data from database. In this way, it could simplify state management compared with retrieving data in componentDidMount stage.

##Install

npm install react-path --save
reactPATH({
	reactName:Subject,//Subject is the name you used for your react component
	htmlName:"root",//root is the div id you used in html to hold your react component
	dataUrl:"/api/getSubject"//"/api/getSubject" is the route you used in backend to retrieve related data from database
});

This module will do two things: 1. Send a post requirement to dataUrl you gave with a value params. The value params would equal to any words after your last "/" in the link address: If your link address is "google.ca/subject", then value = "subject" if your link address is "google.ca/subject/0", then value = "0" 2. After retrieving related data from backend, this module will inital a reactDOM for you with original data like: ReactDOM.render(<Subject data={data} />, document.getElementById("root")); Then you could use this.props.data in your Subject component without considering state issues related to componentDidMount

##Example Assume coding for a sub page with url like "localhost:8000/subject/0". I'll use Flask for backend here, it would be same for other frameworks.

step 1: In backend, code a route rule for /subject/id

@view_pages.route('/subject/<id>')
def subject(id):
    return render_template('subject.html')

step 2: In html, create a div to hold your react root component

<div id="root"></div>
<script type="text/javascript" src="/source/subject.bundle.js"></script>

step3: In React, create your React root component, and inital it by react path

import React, {Component} from 'react';
import reactPATH from 'react-path';
...
class Subject extends Component{
    constructor(props){
		super(props);
		this.state={
			data:this.props.data
		};
	}
}
reactPATH({
	reactName:Subject,
	htmlName:"root",
	dataUrl:"/api/getSubject"
});

step4: In backend, create the "/api/getSubject" route to send related data back to React.

@api_pages.route('/api/getSubject', methods=['GET', 'POST'])
def showSubject():
    if request.method == 'POST':
        id = request.form['value']
        subject = db.subject.find_one({'id':int(id)})
        if not subject:
            abort(404)
        return json_util.dumps(subject)

Work Flow: When localhost:8000/subject/0 has been entered, react-path would send an AJAX post to '/api/getSubject' route in backend with value = 0. Then backend route should retrieve related data from database with value = 0 and send it back as data variable. After react-path get the data, it would inital reactDOM with the name you gave, "Subject" here Inside Subject component, you could use the original data by this.props.data;

##Other This module just stand for an idea to retrieving data in Ajax first and then initial ReactDOM with data in order to simplify state management when you don't want to use react-route and redux in small projects.