
Security News
AGENTS.md Gains Traction as an Open Format for AI Coding Agents
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
@iris-platform/incontext
Advanced tools
Light state management library based on React context API.
Incontext attempts to mimic react-redux interface. While the interface is very similar there some minor differences to watch out, primarily in area of creating Provider's store.
To see how to use incontext
in React application see examples/todo
. This
todo app is virtually copied line by line from redux.js.org website and shows
how easy it is to use incontext
instead of react-redux.
This library uses React's context APIs so it requires React 16.3 or higher.
To install incontext
:
npm install --save incontext
You should wrap your React application with Provider component. Provider component expects store property to be passed to it. Store contains reducers as well as initial state for each reducer.
Use createStore
utility function to convert your reducers to proper store object:
import { createStore } from '@iris-platform/incontext';
const store = createStore([Todo, User, Teams]);
Here Todo, User, Teams are reducers. createStore
accepts array of reducer objects
and converts them to store representation.
import { Provider, createStore } from '@iris-platform/incontext';
const store = createStore([Todo]);
const App = () => (
<Provider store={store}>
...
</Provider>
);
Reducer objects describe how state changes in response to actions.
const initialState = {
visibilityFilter: SHOW_ALL,
todos: []
};
const Todo = (state = initialState, action) => {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return {
...state,
visibilityFilter: action.filter
};
case ADD_TODO:
return {
...state,
todos: [
...state.todos,
{
text: action.text,
completed: false
}
]
};
case TOGGLE_TODO:
return {
...state,
todos: state.todos.map((todo, index) => {
if (index === action.index) {
return {
...todo,
completed: !todo.completed
};
}
return todo;
})
};
default:
return state;
}
};
export default Todo;
You will always need to define initialState
with initial values. Provider will
use these values to set its initial state.
Just like in react-redux we do not mutate the state we copy the state object with updated values.
Actions are functions that deliver specific payloads to incontext
store.
Incontext store updates in response to the reducer receiving the new payload
from incoming action.
const TodoActions = {
addTodo: text => {
return {
type: ADD_TODO,
text
};
},
toggleTodo: index => {
return {
type: TOGGLE_TODO,
index
};
},
setVisibilityFilter: filter => {
return {
type: SET_VISIBILITY_FILTER,
filter
};
}
};
Incontext provides the connect
utility function for creating container components from presentational components. Below is an example usage. See the redux documentation for a complete discussion of component types.
import { connect } from '@iris-platform/incontext';
const MyComponent = ({ name, onClick }) => (
<div onClick={onClick}>Hello {name}!</div>
)
const mapStateToProps = state => ({
name: state.firstName,
})
const mapDispatchToProps = dispatch => ({
onClick: name => dispatch({ type: 'CLICK', name }),
});
const mergeProps = (stateProps, dispatchProps, ownProps) => ({
...stateProps,
...dispatchProps,
onClick: () => dispatchProps.onClick(stateProps.name),
})
export default connect(
mapStateToProps,
mapDispatchToProps,
mergeProps,
)(MyComponent);
Checkout examples folder for sample usage of incontext
.
If you find a bug, please create an issue providing instructions to reproduce it. It's always very appreciable if you find the time to fix it. In this case, please submit a PR.
FAQs
Light state management based on React context API
We found that @iris-platform/incontext demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
AGENTS.md is a fast-growing open format giving AI coding agents a shared, predictable way to understand project setup, style, and workflows.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.