React Hooks
Basic HooksuseState1const [state, setState] = useState(initialState)
Returns a stateful value and a function to update it.
During subsequent re-renders, the first value returned by useState will always be the most recent state after applying updates.
Functional updatesIf the new state is computed using the previous state, you can pass a function to setState. The function will receive the previous value and return an updated value.
Note: Unlike the setState method found in class component, useS ...
5 Tips in [email protected]
Original
It’s fucking long time not learning React.
Five Tips
Displaying Lint Error in the Editor
Create .eslintrc with content of
123{ "extends: "react-app"}
Formatting Code Automatically
1npm install --save-dev prettier husky lint-staged
And add scripts in package.json
12345678910{ "husky": { "hooks": { "pre-commit": "lint-staged" } }, "lint-staged": { "src/**/* ...
Analysis of Ujo
What is UjoThe Ujo platform uses blockchain technology to create a transparent and decentralized database of rights and rights owners, automating royalty payments using smart contracts and cryptocurrency.
Ujo Music is a ConsenSys spoke, with a vision for a music industry that allows creators to grow and thrive independently. It is a platform that uses the ethereum blockchain as the substrate for innovation by empowering artists, digitizing their music rights and metadata, sharing this informatio ...
Development of Neuron Web Extension
Create Project1create-react-app neuron-web scripts-version=react-scripts-ts
Add Manifest123456789101112131415161718192021222324// ./public/manifest.json{ "short_name": "NeuronWeb", "name": "Neuron Web", "start_url": "./index.html", "display": "standalone", "theme_color": "#000", "background_color": "#fff", "browser_action": { ...
Details in Web3 -- Contract
Basic Usage12345// To initialize a contractvar Contract = require('web3-eth-contract')Contract.setProvider('ws://localhost:8546')var contract = new Contract(abi, address, options)
Import123456789var _ = require('underscore')var core = require('web3-core')var Method = require('web3-core-method')var utils = require('web3-utils')var Subscription = require('web3-core-subscription').subscriptionvar formatters = require('web3- ...
RxjsV6
animationFrame - scheduler1const animationFrame: any;
Perform task when window.requestAnimationFrame would fire.
When animationFrame scheduler is used with delay, it will fall back to async scheduler behaviour.
Without delay, animationFrame scheduler can be used to create smooth browser animations. It makes sure scheduled task will happen just before next browser content repaint, thus performing animations as efficiently as possible.
Content Parser in CITA
The parse now we used is
1234567891011const parser = (content: string) => { const bytes = hexToBytes(content) const decoded = pb.UnverifiedTransaction.deserializeBinary(bytes) const tx = decoded.getTransaction() return { from: tx.getFrom ? tx.getFrom() : '', to: tx.getTo ? tx.getTo() : '', data: tx.getData ? tx.getData() : '', value: tx.getValue ? tx.getValue().toString() : '' }}
Here we use these methods:
hexToByt ...
GenServer in Erlang
Original
GenServer is essential part of OTP, which simplifies repeating tasks, letting programmer concentrate on logic of the application, and not on handling edge cases and repeated error handling.
The idea behind GenServer is simple - you start separate process, that holds some state, then on each incoming message(be that call or cast) it may change it internal state and also generate some response(in case of call)
In this manual calling process is named Alice and newly process is Bob.
Progra ...
Association in Phoenix
Original
AssociationsAssociations in Ecto are used when two difference sources(tables) are linked via foreign keys.
A classic example of this setup is “Post has many comments”. First create the two tables in migrations
12345678910111213create table(:posts) do add :title, :string add :body, :text timestamps()endcreate table(:comments) do add :post_id, references(:posts) add :body, :text timestamps()end
Each comment contains a post_id column that by default points to a post id
And now defin ...
Difference Bwtween Build_assoc, Put_assoc, and Cast_assoc
Cast Assoc code
cast_assoc(changeset, name, opts \ [])
Casts the given association with the changeset params
This function should be used when working withe the entire association at once(and not a single element of a many-style association) and using data external to the application.
When updating the data, this function requires the association to have been preloaded in the changeset struct. Missing data will invoke the :on_replace behaviour defined on the association. Preloading is not neces ...
Introduction to Object.getOwnPropertyDescriptors
Object.getOwnPropertyDescriptorsThis method returns all properties including getter and setter.
Object.assign shallow copies all the properties excluding getter and setter of the original source object.
123456789101112131415161718const car = { name: 'BMW', price: 100000, get discount () { return this.d } set discount (x) { this.d = x }}Object.getOwnPropertyDescriptor(car, 'discount') // {get, set, enumerable, configurable}co ...
Enable Sass/scss in Phoenix
Step 1: Add brunch1cd assets && yarn add sass-brunch
Step 2: Add plugin in brunch-config.js123456// brunch-config.jsplugins: { sass: { mode: 'native', },}
Step3: Enable hot-reload1234567891011// dev.exs// add sass and scss to the patternconfig :citaDappStore, CitaDappStoreWeb.Endpoint, live_reload: [ patterns: [ ~r{priv/static/.*(js|css|png|jpeg|jpg|gif|svg|sass|scss)$}, ~r{priv/gettext/.*(po)$}, ~r{lib/cita ...
Model Generator of Rails
Original
Basic Usage1rails g model User email
This command will generate user model with email field type of string, migration which creates user table, test for model and factory.
If you want to have model with different type of string pass type after field name following by :.
The whole list of available types:
123456789101112integerprimary_keydecimalfloatbooleanbinarystringtextdatetimedatetimetimestamp
You are able to pass -option parameter to generator. It will inherit generating class fro ...
Error Handling in Solidity
Solidity uses state-reverting exceptions to handle errors. Such an exception will undo all changes made to the state in the current call(and all its sub-calls) and also flag an error to the caller. The convenience function assert and require can be used to check for conditions and throw an exception if the condition is not met.
The assert function should only be used to test for internal errors, and to check invariants.
The require function should be used to ensure valid conditions, such as inpu ...
Start in Drizzle
Drizzle is a collection of front-end libraries that make wirting dapp front-ends easier and more predicable. The core of Drizzle is based on Redux Store, so you can access to the spectacular development tools around Redux. We take care of synchronizing your contract data, transaction data and more. Things stay fast because you declare what to keep in sync.
Fully reactive contract data, including state, events and transactions
Declarative, so you’re not wasting valuable cycles on unneeded data. ...
Constant, View, and Pure in Solidity
Summary
pure for functions: Disallows modifition or access of state - this is not enforeced yet.
view for functions: Disallow modifition of state - this is not enforced yet.
payable for functions: Allows them to receive Ether together with a call.
constant for state variables: Disallow assignment (except initialization), does not occupy storage slot.
anonymouse for events: Does not store event signature as topic(indexable).
indexed for event parameters: Stores the parameter as topic(indexab ...
Writing Upgradable Contracts in Solidity
Original
Ethereum contracts are immutable – once deployed to the blockchain they cannot be updated, yet the need to change their logic with time is ultimately necessary.
During a contract upgrade the following factors need to be considered:
Block gas limit(4712388 for Homestead)
Upgrade transaction tend to be large due to the amount of processing they have to complete e.g. deploy a contract, move data, move references.
Inter-contract dependencies
when a contract is compiled, all of its imports ...
Dockerizing a React App
Original
Project SetupInstall create-react-app
1npm install -g [email protected]
Creating a new app
12create-react-app docker-appcd docker-app
DockerAdd a Dockerfile to the project root
1234567891011121314151617# base imageFROM node:9.6.1# set working directoryRUN mkdir /usr/src/appWORKDIR /usr/src/app# add `/usr/src/app/node_modules/.bin` to $PATHENV PATH /usr/src/app/node_modules/.bin:$PATH# install ...
Deploy RoR With Mina
Mina SetupLet’s take a look at setting up Mina with Puma. First, you’ll need to add Mina and mina-puma in Gemfile.
Then install gems and execute the initial Mina Command for generating a config/deploy.rb.
12bundlemina init
Detailed Explanations for the Mina deploy file12345678910111213141516171819202122232425262728293031# Set the domain or ip address of the remote server.set :domain, 'yourdomain'# Set the folder of the remote server where Mina will deploy your app.set :deploy_to, ...
What Blockchain Came With
MindMap