GitHub - leebyron/react-loops: React Loops works with React Hooks as part of the...
source link: https://github.com/leebyron/react-loops
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
README.md
React Loops — React Velcro Architecture
React Loops work alongside React Hooks as part of the novel React Velcro architecture for building sticky, secure user interfaces that don't come apart under pressure.
Get started with Velcro by installing React Loops!
yarn add react-loops
For Loops
Use the props of
to provide the list and as
to provide an element for
each item in the list. The of
prop accepts Arrays, Array-likes,
and Iterables.
<ul> <For of={myList} as={item => <li>{item}</li> }/> </ul>
Or provide a "render prop" function as a child.
<ul> <For of={myList}> {item => <li>{item}</li> } </For> </ul>
Access additional information about each iteration by destructuring the second argument:
index
: A number from 0 to the length of the listlength
: The length of the listkey
: The key for this item in the list. Same asindex
for Arrays but string properties forin
ObjectsisFirst
: True for the first iterationisLast
: True for the last iteration
<ul> <For of={myList} as={(item, { isLast }) => <li><If test={isLast}>and </If>{item}</li> }/> </ul>
For in Loops
Use the prop in
to provide an Object instead of an Array or Iterable.
<ul> <For in={myObj} as={(item, {key}) => <li>{key}: {item}</li> }/> </ul>
React Keys
Provide key
on each child to ensure correct behavior if the list may be
reordered over time. If you don't provide key
, the key
of each
iteration will be used by default.
<ul> <For of={myList} as={item => <li key={item.id}>{item.label}</li> }/> </ul>
If conditions
Use the test
prop with <If>
and <ElseIf>
elements to conditionally
include certain elements. When an <If>
test is truthy it does not
render any <ElseIf>
or <Else>
children. However when it is falsey it
only renders <ElseIf>
and <Else>
children.
<If test={someCondition}> This will only be shown if someCondition is truthy. <ElseIf test={otherCondition}> This will only be shown if someCondition is falsey and otherCondition is truthy. <Else> This will only be shown if both someCondition and otherCondition are both falsey. </Else> </ElseIf> <Else> This will be shown if someCondition is falsey. <If test={finalCondition}> This will be shown if someCondition is falsey and finalCondition is truthy. </If> </Else> </If>
Alternatively, you can provide then
and else
props.
<If test={someCondition} then={"This will only be shown if someCondition is truthy."} else={"This will be shown if someCondition is falsey."} />
What is React Velcro?
Only the newest, coolest, most blazing fast React architecture out there!
React Hooks has been an exciting development in the evolution of React, but it felt like it was only half of the story. React Loops completes the gripping picture by providing React's missing control-flow operators via JSX elements.
Is this a Joke?
Take a look at this side by side with the old looping pattern and you tell me.
Recommend
-
37
Hooks are a new feature proposal that lets you use state and other React features without writing a class. They’re currently in React v16.7.0-alpha and being discussed in
-
47
A JavaScript library for building user interfaces
-
17
Wondering how to create loops in React? Learn about JSX and how to use methods like the map function to loop inside React JSX and render a list of items. JSX in a Nutshell If you have worked with React b...
-
4
Files Permalink Latest commit message Commit time
-
1
Copy link Starwort commented
-
2
Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run:
-
6
react-createForm Create hooks to manage your forms. React create form is experimental open source project that allow you to create form easily, different from the others options, this package guide y...
-
4
React Factor Hoist React Hooks up to React Contexts to create shared dynamic states. Try ou...
-
3
React ML Kit React ML Kit is a collection of React hooks that allow you to easily integrate AI functionalities using machine learning models directly in the browser. These hooks are designed to be user-friendly and efficient, providing de...
-
0
I recently ran into a very nasty little conundrum with React. I found a use-case where I felt it was necessary to update React state variables inside a loop. I initially had no idea how vexing it would be.
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK